JAVA This program is for infix to postfix calculation I need
(JAVA) This program is for infix to postfix calculation.
I need help on few points
1. I need code for ActionListener in Main.java
2. DivisionException seems not work properly when I divide the number by 0.
3. And any other parts I have an error on.
Thank you in advance.
//Main.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class Main extends JFrame{
private GridLayout glm = new GridLayout(3, 1, 5, 20);
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JPanel jp3 = new JPanel();
private GridLayout gl1 = new GridLayout (1, 2);
private JLabel jl1 = new JLabel(\"Enter Infix Expression\", JLabel.CENTER);
private JTextField jtf1 = new JTextField();
private GridLayout gl2 = new GridLayout(1, 3, 5, 9);
private JButton jbEvaluate = new JButton(\"Evaluate\");
private GridLayout gl3 = new GridLayout(1, 2);
private JLabel jl2 = new JLabel(\"Result\", JLabel.CENTER);
private JTextField jtf2 = new JTextField();
private int result;
public String userInput(){
return jtf1.getText();
}
public Main(){
setTitle(\"Infix Expression Evaluator\");
setSize(500, 200);
setLayout(glm);
jp1.setLayout(gl1);
jp1.add(jl1);
jp1.add(jtf1);
add(jp1);
jp2.setLayout(gl2);
jp2.add(new JLabel(\"\"));
jp2.add(jbEvaluate);
jp2.add(new JLabel(\"\"));
add(jp2);
jp3.setLayout(gl3);
jp3.add(jl2);
jp3.add(jtf2);
jtf2.setEditable(false);
jtf2.setBackground(Color.lightGray);
add(jp3);
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
}
class EnterActionListener implements ActionListener{
public void actionPerformed(ActionEvent ae){
result = Evaluation.evaluate(userInput());
jtf2.setText(Integer.parseInt(result));
}
}
public static void main (String[] args){
Main main = new Main();
}
}
===================================
//Evaluation.java
import java.util.*;
import javax.swing.JOptionPane;
public class Evaluation {
public static int evaluate(String expression){
char[] tokens = expression.toCharArray();
//Stack for numbers
Stack<Integer> operandStack = new Stack<Integer>();
//Stack for Operators
Stack<Character> operatorStack = new Stack<Character>();
//using for
for(int i = 0; i < tokens.length; i++){
if(tokens[i] == \' \'){
JOptionPane.showMessageDialog(null, \"Blank space is unacceptable\", \"Spacing Error\", JOptionPane.WARNING_MESSAGE);
}
//Token is a number, push it to stack
if(tokens[i] >= \'0\' && tokens[i] <=\'9\'){
//There can be multi digit number. create number as string
StringBuffer numberString = new StringBuffer();
while(i < tokens.length && tokens[i] >= \'0\' && tokens[i] <= \'9\'){
numberString.append(tokens[i++]);
}
//converting numberString to integer and adding to array
operandStack.push(Integer.parseInt(numberString.toString()));
}
//Encounter open bracket, push it to operator stack
else if(tokens[i] == \'(\'){
operatorStack.push(tokens[i]);
}
//Encounter closing bracket, evaluate and push result back in operand stack
else if(tokens [i] == \')\'){
while(operatorStack.peek()!=\'(\')
try {
operandStack.push(evaluateOperation(operatorStack.pop(), operandStack.pop(), operandStack.pop()));
} catch (DivisionException de) {
new DivisionException();
}
operatorStack.pop();
}
//Encounter a operator
else if(tokens[i] == \'+\' || tokens[i] == \'-\' || tokens[i] == \'*\' || tokens[i] == \'/\'){
while(!operatorStack.empty() && isHigherPrecedence(tokens[i], operatorStack.peek())){
try {
operandStack.push(evaluateOperation(operatorStack.pop(), operandStack.pop(), operandStack.pop()));
} catch (DivisionException de) {
new DivisionException();
}
}
//Push current token to \'op\'
operatorStack.push(tokens[i]);
}
}
while(!operatorStack.empty()){
try {
operandStack.push(evaluateOperation(operatorStack.pop(), operandStack.pop(), operandStack.pop()));
} catch (DivisionException de) {
new DivisionException();
}
}
//operandStack now contain result
return operandStack.pop();
}
//check for precedence order
public static boolean isHigherPrecedence(char op1, char op2){
if(op2 == \'(\' || op2 == \')\') return false;
if((op1 == \'*\' || op1 == \'/\') && (op2 == \'+\' || op2 == \'-\')) return false;
else return true;
}
//evaluate result of operator on operand b and a
public static int evaluateOperation(char op, int b, int a) throws DivisionException{
try{
switch(op){
case \'+\': return a+b;
case \'-\': return a-b;
case \'*\': return a*b;
case \'/\':
if(b == 0){
throw new DivisionException();
}
else return a/b;
}
}catch(DivisionException de){
new DivisionException();
}
return 0;
}
public static void main(String[] args){
System.out.println(Evaluation.evaluate(\"900/0\"));
System.out.println(Evaluation.evaluate(\"(3+4/2)+6/2-7\"));
}
}
==================
//DivisionException.java
import javax.swing.JOptionPane;
public class DivisionException extends Exception{
public DivisionException(){
JOptionPane.showMessageDialog(null, \"Division Error\", \"Cannot divide a number by 0\", JOptionPane.WARNING_MESSAGE);
}
}
Solution
For division exception you need to throw it back if you are catching it.
catch(DivisionException de){
throw new DivisionException();
}



