无效字符异常不捕获字符
InvalidCharacterException not catching characters
我正在为一个 class 做作业,如果用户输入了无效字符,它要求我抛出 RuntimeException。此输入将用于将 post 固定表示法中的表达式转换为中缀。现在,我已经完成了我的任务,但我无法正确捕获 RuntimeException。我尝试创建一个 InvalidCharacterException,但它也没有捕捉到任何东西。
我要问的是为什么我的方法没有捕获字符并抛出异常
这是我的class供参考
post修复表达式
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();
}
}
树生成器
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;
}
}
节点class
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;
}
}
您的 isInteger
方法总是 return 正确。所以你永远不会检测到任何无效字符,因此永远不会抛出异常。
怎么可能?对于大多数无效字符,Character.getNumericValue(c)
将 return -1。您将此数字转换为 Integer
实例,然后测试它是否为 Integer
。这是。它甚至被声明为一个整数,所以测试永远不会失败。所以你的方法 returns true
。回到您的 buildTree
方法,if
条件将为假并且您的 throw
语句未达到。
如果您从 Character.getNumericValue(c)
得到否定结果,我的直接修复想法是 isInteger
到 return false
的方法。在做出最终决定之前,您可能需要再次阅读该方法的文档。
我正在为一个 class 做作业,如果用户输入了无效字符,它要求我抛出 RuntimeException。此输入将用于将 post 固定表示法中的表达式转换为中缀。现在,我已经完成了我的任务,但我无法正确捕获 RuntimeException。我尝试创建一个 InvalidCharacterException,但它也没有捕捉到任何东西。
我要问的是为什么我的方法没有捕获字符并抛出异常 这是我的class供参考
post修复表达式
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();
}
}
树生成器
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;
}
}
节点class
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;
}
}
您的 isInteger
方法总是 return 正确。所以你永远不会检测到任何无效字符,因此永远不会抛出异常。
怎么可能?对于大多数无效字符,Character.getNumericValue(c)
将 return -1。您将此数字转换为 Integer
实例,然后测试它是否为 Integer
。这是。它甚至被声明为一个整数,所以测试永远不会失败。所以你的方法 returns true
。回到您的 buildTree
方法,if
条件将为假并且您的 throw
语句未达到。
如果您从 Character.getNumericValue(c)
得到否定结果,我的直接修复想法是 isInteger
到 return false
的方法。在做出最终决定之前,您可能需要再次阅读该方法的文档。