Hey guys I am trying to get my post-fix expression code to work with a float after I have already created it using an integer, the reason is so that I can get decimal places when I divide. The problem is that I am getting a run time error and I am not sure why. Here is my original code using integers (THAT WORKS) and following is the switch to float. Appreciate any help. Thanks
import java.util.Stack; import java.util.StringTokenizer; public class P2Evaluate { private final char ADD = '+'; private final char SUBTRACT = '-'; private final char MULTIPLY = '*'; private final char DIVIDE = '/'; Stack<Integer> stack = new Stack<Integer>(); public int evaluate (String expr) { int op1 = 0; int op2 = 0; int result = 0; String token; StringTokenizer tokenizer = new StringTokenizer(expr); while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); if (isOperator(token)) { op2 = (stack.pop()).intValue(); op1 = (stack.pop()).intValue(); result = evalSingleOp (token.charAt(0), op1, op2); stack.push (new Integer(result)); } else { stack.push (new Integer(Integer.parseInt(token))); } } return result; } private boolean isOperator (String token) { return (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")); } private int evalSingleOp (char operation, int op1, int op2) { int result = 0; switch (operation) { case ADD: result = op1+op2; break; case SUBTRACT: result = op1-op2; break; case MULTIPLY: result = op1*op2; break; case DIVIDE: result = op1/op2; } return result; } }import java.util.Stack; import java.util.StringTokenizer; public class P2Evaluate { private final char ADD = '+'; private final char SUBTRACT = '-'; private final char MULTIPLY = '*'; private final char DIVIDE = '/'; Stack<Float> stack = new Stack<Float>(); public float evaluate (String expr) { float op1 = 0; float op2 = 0; float result = 0; String token; StringTokenizer tokenizer = new StringTokenizer(expr); while (tokenizer.hasMoreTokens()) { token = tokenizer.nextToken(); if (isOperator(token)) { op2 = (stack.pop()).floatValue(); op1 = (stack.pop()).floatValue(); result = evalSingleOp (token.charAt(0), op1, op2); stack.push (new Float(result)); } else { stack.push (new Float(Float.valueOf(token).floatValue())); } } return result; } private boolean isOperator (String token) { return (token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")); } private float evalSingleOp (char operation, float op1, float op2) { float result = 0; switch (operation) { case ADD: result = op1+op2; break; case SUBTRACT: result = op1-op2; break; case MULTIPLY: result = op1*op2; break; case DIVIDE: result = op1/op2; } return result; } }Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: P2Evaluate.e valuate(Ljava/lang/String;)I at P2Control$solveListener.actionPerformed(P2Control.java:55) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:20 18) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.jav a:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel .java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259 ) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL istener.java:252) at java.awt.Component.processMouseEvent(Component.java:6504) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6269) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4860) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4686) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832 ) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2713) at java.awt.Component.dispatchEvent(Component.java:4686) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707) at java.awt.EventQueue.access$000(EventQueue.java:101) at java.awt.EventQueue$3.run(EventQueue.java:666) at java.awt.EventQueue$3.run(EventQueue.java:664) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo main.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo main.java:87) at java.awt.EventQueue$4.run(EventQueue.java:680) at java.awt.EventQueue$4.run(EventQueue.java:678) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDo main.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:677) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre ad.java:211) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread. java:128) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre ad.java:117) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105) at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)