Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Switch from integer to a float

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch from integer to a float

    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)


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Switch from integer to a float

    I am getting a run time error
    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch from integer to a float

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    I corrected the formatting

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Switch from integer to a float

    java.lang.NoSuchMethodError: P2Evaluate.evaluate(Ljava/lang/StringI
    at P2Control$solveListener.actionPerformed(P2Control. java:55)
    That type of error normally happens at compile time. It looks like a class was changed after another class was compiled using the earlier version.

    Try recompiling all the classes and test again.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Switch from integer to a float

    That did the trick. Thanks.

  6. #6
    Member Zyrion's Avatar
    Join Date
    Feb 2013
    Location
    Iowa
    Posts
    106
    My Mood
    Angelic
    Thanks
    2
    Thanked 8 Times in 8 Posts

    Default Re: Switch from integer to a float

    Never mind. I see your issue is resolved.

Similar Threads

  1. [SOLVED] Extracting whole number digits from a float
    By Johnny Bravo in forum Algorithms & Recursion
    Replies: 2
    Last Post: August 12th, 2012, 04:04 PM
  2. issues with a float
    By 2k. in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 30th, 2011, 12:03 PM
  3. float to 2 decimal places
    By fh84 in forum Java Theory & Questions
    Replies: 3
    Last Post: November 25th, 2009, 11:27 AM
  4. How to check that console input should be integer only?
    By Konnor in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: February 2nd, 2009, 05:37 AM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM