I am trying to write a basic four-function calculator. Here is the code:
import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; class SingleObjectCalculator extends JFrame { JTextField screen = new JTextField("0",15); JButton one; JButton two; JButton three; JButton four; JButton five; JButton six; JButton seven; JButton eight; JButton nine; JButton zero; JButton point; JButton plus; JButton minus; JButton times; JButton divide; JButton equals; JButton clear; Stack<String> opndStck = new Stack<String>(); Stack<String> optrStck = new Stack<String>(); class ButtonListener implements ActionListener { boolean ptPshd = false; boolean optrPshd = false; double fOpnd = 0; double sOpnd = 0; double res = 0; String exprStr = ""; String optrStr = ""; String curOpndStr = ""; String prvOpndStr = ""; String curOptrStr = ""; String prvOptrStr = ""; String prvPrvOptrStr = ""; public void actionPerformed (ActionEvent ae) { String butStr = ae.getActionCommand(); if (butStr.equals("0")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } } else { exprStr = butStr; } optrPshd = false; } else if (butStr.equals("1")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("2")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("3")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("4")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("5")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("6")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("7")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("8")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals("9")) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } else if (butStr.equals(".")) { if (!ptPshd) { if (!optrPshd) { if (exprStr.equals("0")) { exprStr += butStr; screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } } else { if (exprStr.length() != 0) { exprStr = ("0" + butStr); screen.setText(exprStr); } else { exprStr += butStr; screen.setText(exprStr); } optrPshd = false; } } ptPshd = false; } else if (butStr.equals("+")) { optrStr = butStr; ptPshd = false; if (!optrPshd) { if (opndStck.empty() && optrStck.empty()) { opndStck.push(screen.getText()); optrStck.push(optrStr); } else if (!opndStck.empty() && !optrStck.empty()) { curOpndStr = screen.getText(); curOptrStr = optrStr; } optrPshd = true; } else { optrStck.pop(); optrStck.push(optrStr); } if (opndStck.size() > 0 && optrStck.size() > 0) { prvOpndStr = opndStck.pop(); prvOptrStr = optrStck.pop(); sOpnd = Double.parseDouble(curOpndStr); fOpnd = Double.parseDouble(prvOpndStr); System.out.println(sOpnd); System.out.println(fOpnd); while (opndStck.size() > 1 && optrStck.size() > 1) { if (opndStck.size() == 1 && optrStck.size() == 1) { if (prvOptrStr.equals("+")) { res = fOpnd + sOpnd; System.out.println(res); } else if (prvOptrStr.equals("-")) { res = fOpnd - sOpnd; System.out.println(res); } } else { prvOpndStr = opndStck.pop(); prvOptrStr = optrStck.pop(); prvPrvOptrStr = optrStck.pop(); sOpnd = Double.parseDouble(curOpndStr); fOpnd = Double.parseDouble(prvOpndStr); if (prvOptrStr.equals("*")) { res = fOpnd * sOpnd; System.out.println(res); } else if (prvOptrStr.equals("/")) { res = fOpnd / sOpnd; System.out.println(res); } } opndStck.push(res + ""); } screen.setText(res + ""); } else if (opndStck.size() <= 1 && optrStck.size() == 1) { prvOpndStr = opndStck.pop(); prvOptrStr = optrStck.pop(); } } else if (butStr.equals("-")) { optrStr = butStr; ptPshd = false; if (!optrPshd) { if (opndStck.empty() && optrStck.empty()) { opndStck.push(screen.getText()); optrStck.push(optrStr); } else { curOpndStr = screen.getText(); curOptrStr = optrStr; } optrPshd = true; } else { optrStck.pop(); optrStck.push(optrStr); } } else if (butStr.equals("*")) { optrStr = butStr; ptPshd = false; if (!optrPshd) { if (opndStck.empty() && optrStck.empty()) { opndStck.push(screen.getText()); optrStck.push(optrStr); } else { curOpndStr = screen.getText(); curOptrStr = optrStr; } optrPshd = true; } else { optrStck.pop(); optrStck.push(optrStr); } } else if (butStr.equals("/")) { optrStr = butStr; ptPshd = false; if (!optrPshd) { if (opndStck.empty() && optrStck.empty()) { opndStck.push(screen.getText()); optrStck.push(optrStr); } else { curOpndStr = screen.getText(); curOptrStr = optrStr; } optrPshd = true; } else { optrStck.pop(); optrStck.push(optrStr); } for (String s : opndStck) { System.out.print(s); } System.out.println(); for (String s : optrStck) { System.out.print(s); } System.out.println(); System.out.println(); } else if (butStr.equals("=")) { optrStr = butStr; ptPshd = false; opndStck.push(screen.getText()); optrStck.push(optrStr); } else if (butStr.equals("C")) { screen.setText("0"); while (!opndStck.empty()) { opndStck.pop(); } while (!optrStck.empty()) { optrStck.pop(); } ptPshd = false; optrPshd = false; fOpnd = 0; sOpnd = 0; res = 0; exprStr = ""; optrStr = ""; curOpndStr = ""; prvOpndStr = ""; prvPrvOptrStr = ""; curOptrStr = ""; prvOptrStr = ""; } } } ActionListener bl = new ButtonListener(); SingleObjectCalculator() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,250); setVisible(true); setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); Insets bi = new Insets(15, 15, 15, 15); gbc.gridwidth = 4; gbc.gridx = 0; gbc.gridy = 0; add(screen, gbc); gbc.gridwidth = 1; seven = new JButton("7"); gbc.ipady = 5; gbc.gridx = 0; gbc.gridy = 1; add(seven, gbc); eight = new JButton("8"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 1; gbc.gridy = 1; add(eight, gbc); nine = new JButton("9"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 2; gbc.gridy = 1; add(nine, gbc); plus = new JButton("+"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 3; gbc.gridy = 1; add(plus, gbc); four = new JButton("4"); gbc.weighty = 0; gbc.gridx = 0; gbc.gridy = 2; add(four, gbc); five = new JButton("5"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 1; gbc.gridy = 2; add(five, gbc); six = new JButton("6"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 2; gbc.gridy = 2; add(six, gbc); minus = new JButton("-"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 3; gbc.gridy = 2; add(minus, gbc); one = new JButton("1"); gbc.weighty = 0; gbc.gridx = 0; gbc.gridy = 3; add(one, gbc); two = new JButton("2"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 1; gbc.gridy = 3; add(two, gbc); three = new JButton("3"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 2; gbc.gridy = 3; add(three, gbc); times = new JButton("*"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 3; gbc.gridy = 3; add(times, gbc); point = new JButton("."); gbc.weighty = 0; gbc.gridx = 0; gbc.gridy = 4; add(point, gbc); zero = new JButton("0"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 1; gbc.gridy = 4; add(zero, gbc); equals = new JButton("="); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 2; gbc.gridy = 4; add(equals, gbc); divide = new JButton("/"); //gbc.fill = GridBagConstraints.RELATIVE; gbc.gridx = 3; gbc.gridy = 4; add(divide, gbc); clear = new JButton("C"); gbc.gridwidth = 4; gbc.gridx = 4; gbc.gridy = 0; add(clear, gbc); one.addActionListener(bl); two.addActionListener(bl); three.addActionListener(bl); four.addActionListener(bl); five.addActionListener(bl); six.addActionListener(bl); seven.addActionListener(bl); eight.addActionListener(bl); nine.addActionListener(bl); zero.addActionListener(bl); point.addActionListener(bl); equals.addActionListener(bl); plus.addActionListener(bl); minus.addActionListener(bl); times.addActionListener(bl); divide.addActionListener(bl); clear.addActionListener(bl); } public static void main (String args[]) { SingleObjectCalculator soc = new SingleObjectCalculator(); } }
Currently it throws a NumberFormatExcpetion at line 400:
sOpnd = Double.parseDouble(curOpndStr);
The JVM claims that it is throwing the exception because the string curOpndStr is empty (as it is when I initialize it at the beginning of actionPerformed()). I am able to trace the error back to a block of code where I think that I reassign curOpndStr a non-empty value.
if (opndStck.empty() && optrStck.empty()) { opndStck.push(screen.getText()); optrStck.push(optrStr); } else if (!opndStck.empty() && !optrStck.empty()) { curOpndStr = screen.getText(); curOptrStr = optrStr; }
I have test that block of code and know that, when the "+" button is pushed for the first time, opndStck.empty() && optrStck.empty() becomes false, I am confused as to why, when I test the code after it curOpndStr is empty.
Is there some way that I am inadvertantly resetting the value?
I apologize for posting such a long code, but I thought it might help.