Question: (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
(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
==================
//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); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
