I'm doing an assignment for a class that requires me to throw a RuntimeException if an invalid character is entered by the user. This input will be used to convert an expression that's in post fix-notation to infix. Now, I have my assignment completed, but I can't get the RuntimeException to catch properly. I tried creating an InvalidCharacterException, but that doesn't catch anything either.
What I'm asking is why my method isn't catching characters and throwing an exception Here are my classes for reference
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 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;
}
}
Your isInteger
method is always returning true. So you never detect any invalid characters and hence never throw your exception.
How can that be? For most invalid characters, Character.getNumericValue(c)
will return -1. You convert this number into an Integer
instance and then test whether it is an Integer
. It is. It’s even declared an integer, so the test could never fail. So your method returns true
. Back in your buildTree
method, the the if
condition will be false and your throw
statement not reached.
My immediate idea for a fix would be in the isInteger
method to return false
if you get a negative result from Character.getNumericValue(c)
. You may want to read the documentation of that method once more before you make a final decision.