2017-02-18 63 views
0

我正在做一個類的分配,如果用戶輸入無效字符,需要我拋出RuntimeException。此輸入將用於將表示修復後符號的表達式轉換爲中綴。現在,我完成了我的任務,但無法正確捕獲RuntimeException。我試着創建一個InvalidCharacterException,但是也沒有捕捉到任何東西。InvalidCharacterException不捕捉字符

什麼我問的是,爲什麼我的方法是不大字,並拋出一個異常 這裏是我的類參考

postfixExpression

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import java.io.*; 
import java.util.*; 

public class postfixExpression extends JPanel 
{ 
    private JFrame frame;//The frame 
    private JPanel panel;//The panel 
    private JLabel enterPostfix; 
    private JTextField postfixExpression; 
    private JButton construct; 
    private JButton blank; 
    private JLabel results; 
    private JTextField resultsDisplay; 





    //Builds the GUI 
    public postfixExpression() 
    { 
     frame=new JFrame("Three Address Generator"); 
     panel=new JPanel(); 
     enterPostfix=new JLabel("Enter Postfix Expression"); 
     postfixExpression=new JTextField(""); 
     construct=new JButton("Construct Tree"); 
     blank=new JButton(); 
     results=new JLabel("Infix Expression"); 
     resultsDisplay=new JTextField(""); 







     construct.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       try 
       { 
        String expression=postfixExpression.getText(); 
        char[] charArray=expression.toCharArray(); 
        treeBuilder et=new treeBuilder(); 
        Node root=et.buildTree(charArray); 
        resultsDisplay.setText(treeBuilder.inorder(root)); 
        root=et.insertRegisters(charArray); 
        et.writeInstructions(root); 
       } 
       catch(InvalidCharacterException i) 
       { 
        JOptionPane.showMessageDialog(null, "Invalid character"); 
       } 

      } 
     }); 



      //Adding the parts together 
     panel.setLayout(new GridLayout(3,2)); 
     panel.add(enterPostfix); 
     panel.add(postfixExpression); 
     panel.add(construct); 
     panel.add(blank); 
     panel.add(results); 
     panel.add(resultsDisplay); 
     frame.add(panel); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setSize(600,300); 
     frame.setBackground(Color.red); 
     frame.setVisible(true);; 





    }   






    //Main method 
    public static void main(String[] args) 
    { 
     postfixExpression myGUI=new postfixExpression(); 
    } 
} 

TreeBuilder作爲

import java.io.*; 
import java.util.*; 
public class treeBuilder 
{ 

//Checks if the current character is an operator 
public boolean isOperator(char c) 
{ 
    return Character.isDigit(c); 
} 

//Checks if the current character is an Integer 
public boolean isInteger(char c) 
{ 
    int test=Character.getNumericValue(c); 
    Integer test2=(Integer)test; 

    if(test2 instanceof Integer) 
    { 
     return true; 
    } 
    return false; 
} 
public static String inorder(Node t) 
{ 
    if(t!=null) 
    { 
     return "("+inorder(t.getLeft())+t.getValue()+" "+inorder(t.getRight())+")"; 
    } 
    return ""; 
} 

public Node buildTree(char postFix[]) throws RuntimeException 
{ 
    Stack<Node> nodeStack=new Stack<Node>(); 
    Node tree=null; 
    Node t1=null; 
    Node t2=null; 

    for(int i=0;i<postFix.length;i++) 
    { 
     if(!isOperator(postFix[i])&&!isInteger(postFix[i])) 
     { 
      throw new InvalidCharacterException(postFix[i]); 

     } 
     if(!isOperator(postFix[i])) 
     { 
      tree=new Node(postFix[i]); 
      nodeStack.push(tree); 
     } 
     else if(isOperator(postFix[i])) 
     { 
      tree= new Node(postFix[i]); 
      t1=nodeStack.pop(); 
      t2=nodeStack.pop(); 
      tree.setRight(t1); 
      tree.setLeft(t2); 
      nodeStack.push(tree); 
     } 
     else if(!isOperator(postFix[i])&& !isInteger(postFix[i])) 
     { 
      throw new InvalidCharacterException(postFix[i]); 
     } 


    } 
    tree=nodeStack.pop(); 
    return tree; 
} 


public Node insertRegisters(char[] postFix) 
{ 
    Stack<Node> nodeStack=new Stack<Node>(); 
    Node tree=null; 
    Node t1=null; 
    Node t2=null; 
    int registerCount=0; 
    for(int i=0;i<postFix.length;i++) 
    { 
     if(!isOperator(postFix[i])) 
     { 
      tree=new Node(postFix[i]); 
      nodeStack.push(tree); 
     } 
     else if(isOperator(postFix[i])) 
     { 
      tree = new Node(postFix[i], "R" + registerCount++); 
      t1 = nodeStack.pop(); 
      t2 = nodeStack.pop(); 
      tree.setRight(t1); 
      tree.setLeft(t2); 
      nodeStack.push(tree); 
     } 

    } 
    return tree; 
} 


public String writeInstructionsHelper(Node root) 
{ 
    String str=""; 
    if(root != null) 
    { 
     if(root.getLeft()!=null && root.getLeft().getRegister() !=null) 
     { 
      str += writeInstructionsHelper(root.getLeft()); 
     } 
     if(root.getRight()!=null && root.getRight().getRegister() !=null) 
     { 
      str += writeInstructionsHelper(root.getRight()); 
     } 

     String instructions=null; 
     if(root.getValue()=='+') 
     { 
      instructions="Add"; 
     } 
     else if(root.getValue()=='-') 
     { 
      instructions="Sub"; 
     } 
     else if(root.getValue()=='*') 
     { 
      instructions="Mul"; 
     } 
     else if(root.getValue()=='/') 
     { 
      instructions="Div"; 
     } 

     if(root.getRegister()==null) 
     { 
      str+=root.getValue(); 
     } 
     else 
     { 
      str += instructions + " "; 
      str += root.getRegisterOrValue() + " "; 
      str += root.getLeft().getRegisterOrValue() + " "; 
      str += root.getRight().getRegisterOrValue(); 
     } 
    } 
    str+="\r\n"; 
    return str; 
} 

public void writeInstructions(Node root) 
{ 
    String file="myFile.txt"; 
    try 
    { 
     String instructions = writeInstructionsHelper(root); 
     PrintWriter outputStream = new PrintWriter(file); 
     outputStream.println(instructions); 
     outputStream.flush(); 
     outputStream.close(); 
    } 
    catch(FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 

} 
} 

InvalidCharacterException

public class InvalidCharacterException extends RuntimeException 
{ 
private char c; 

public InvalidCharacterException(char c) 
{ 
    this.c=c; 
} 

public char getCharacter() 
{ 
    return c; 
} 
} 

Node類

public class Node 
{ 
private char value; 
private String register; 
private Node left; 
private Node right; 

public Node(char value) 
{ 
    this.value=value; 
    left=null; 
    right=null; 
    register=null; 
} 
public Node(char value, String register) 
{ 
    this.value = value; 
    this.register = register; 
    left = null; 
    right = null; 
} 
public char getValue() { 
    return value; 
} 

public void setValue(char value) { 
    this.value = value; 
} 

public String getRegister() { 
    return register; 
} 

public void setRegister(String register) { 
    this.register = register; 
} 

public Node getLeft() { 
    return left; 
} 

public void setLeft(Node left) { 
    this.left = left; 
} 

public Node getRight() { 
    return right; 
} 

public void setRight(Node right) { 
    this.right = right; 
} 

public String getRegisterOrValue() 
{ 
    if (register == null) 
    { 
     return "" + value; // must convert to string 
    } 
    else 
    { 
     return register; 
    } 
} 



public String toStringHelper(int indents) { 
    String str = ""; 

    str += "value: " + value + "\n"; 

    for (int i = 0; i < indents; i++) { 
     str += "\t"; 
    } 
    if (left == null) 
     str += "LEFT: null\n"; 
    else 
     str += "LEFT: " + left.toStringHelper(indents + 1) + "\n"; 

    for (int i = 0; i < indents; i++) { 
     str += "\t"; 
    } 
    if (right == null) 
     str += "RIGHT: null\n"; 
    else 
     str += "RIGHT: " + right.toStringHelper(indents + 1) + "\n"; 

    return str; 
} 



} 
+0

爲什麼不使用Exeception將它作爲最後的手段來捕捉它,還是明確地使用了你提到的那些? –

+0

這個任務說我必須在主類中拋出一個RuntimeException,它是定義GUI的那個,我相信 – WILLO567

+0

你試過調試器嗎? –

回答

0

isInteger方法總是返回true。所以你永遠不會檢測到任何無效字符,因此永遠不會拋出你的異常

這怎麼可能?對於大多數無效字符,Character.getNumericValue(c)將返回-1。您將此號碼轉換爲Integer實例,然後測試它是否爲Integer。它是。它甚至被聲明爲一個整數,所以測試永遠不會失敗。所以你的方法返回true。回到buildTree方法中,if條件將爲false,並且未達到您的throw聲明。

如果您從Character.getNumericValue(c)得到否定結果,我的解決方法的直接想法是在isInteger方法中返回false。在做出最終決定之前,您可能希望再次閱讀該方法的文檔。

+1

我看到你在說什麼,但是字符a-z和A-Z都有數字值,所以它們不會返回爲-1 – WILLO567

+0

True。也許你應該使用'Character.isDigit()'來代替? @ WILLO567 –

+0

我剛剛嘗試使用Character.isDigit(),它仍然沒有捕獲任何字母並拋出異常。它是在有操作員的情況下完成的,但只有當操作員領導表達時纔會這樣做。作爲參考,我的下一個if語句是:else if(!isOperator(postFix [i])&&!isInteger(postFix [i]) – WILLO567