4 Replies - 132 Views - Last Post: 19 February 2019 - 11:29 AM Rate Topic: -----

#1 John58364   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-October 18

Number Format Exception

Posted 19 February 2019 - 10:25 AM

import java.util.Stack;
import java.util.Scanner;

public class PostfixToInfixTranslator
{
	private final static char ADD = '+';
	private final static char SUBTRACT = '-';
	private final static char MULTIPLY = '*';
	private final static char DIVIDE = '/';
	private Stack<Integer> stack;
	
	public PostfixToInfixTranslator()
	{
		stack = new Stack<Integer>();
	}
	
	public String evaluate(String expr)
	{
		int op1, op2;
		String result = "";
		String token;
		Scanner parser = new Scanner(expr);
		while (parser.hasNext()) 
		{
			token = parser.next();
			if (isOperator(token))
			{
				op2 = (stack.pop()).intValue();
				String option = Integer.toString(op2);
				op1 = (stack.pop()).intValue();
				String option2 = Integer.toString(op1);
				result = evaluateSingleOperator(token.charAt(0), option2, option);
				stack.push(new Integer(result));
				String newResult = result.toString();
			}
			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 String evaluateSingleOperator(char operation, String op1, String op2)
	{
		String result = "";
		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 + ")";
			break;
		}
		return result;
	}
}



import java.util.Scanner;

public class PostFixTester
{

	public static void main(String[] args)
	{
		String expression, again;
		String result;
		
		Scanner in = new Scanner(System.in);
		
		do
		{
			PostfixToInfixTranslator evaluator = new PostfixToInfixTranslator();
			System.out.println("Enter a valid post-fix expression one token " +
							   "at a time with a space between each token " +
							   "(e.g. 5 4 + 3 2 1 - + *)");
			
			System.out.println("Each token must be an integer or an " +
								"operator (+,-,*,/)");
			expression = in.nextLine();
			result = evaluator.evaluate(expression);
			System.out.println();
			System.out.println("That expression equals " + result);
			System.out.print("Evaluate another expression [Y/N]? ");
			again = in.nextLine();
			System.out.println();
		}
		while(again.equalsIgnoreCase("y"));
	}

}



ERRORS:
Exception in thread "main" java.lang.NumberFormatException: For input string: "(3 + 2)"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:569)
at java.lang.Integer.<init>(Integer.java:867)
at PostfixToInfixTranslator.evaluate(PostfixToInfixTranslator.java:33)
at PostFixTester.main(PostFixTester.java:23)

Im not quite sure how to fix this error.

Is This A Good Question/Topic? 0
  • +

Replies To: Number Format Exception

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,828
  • Joined: 13-June 14

Re: Number Format Exception

Posted 19 February 2019 - 10:31 AM

You need to parse the string, not try to interpret it as a single number (since obviously it isn't).
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14845
  • View blog
  • Posts: 59,246
  • Joined: 12-June 08

Re: Number Format Exception

Posted 19 February 2019 - 10:32 AM

Per the error - post your 'evaluate' code.

Quote

at PostfixToInfixTranslator.evaluate(PostfixToInfixTranslator.java:33)
at PostFixTester.main(PostFixTester.java:23)

Was This Post Helpful? 0
  • +
  • -

#4 John58364   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 25-October 18

Re: Number Format Exception

Posted 19 February 2019 - 10:44 AM

View Postmodi123_1, on 19 February 2019 - 10:32 AM, said:

Per the error - post your 'evaluate' code.

Quote

at PostfixToInfixTranslator.evaluate(PostfixToInfixTranslator.java:33)
at PostFixTester.main(PostFixTester.java:23)


Lines 17-40 is the 'evaluate' code. I still don't see where I'm supposed to parse the string...
Was This Post Helpful? 0
  • +
  • -

#5 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3617
  • View blog
  • Posts: 16,593
  • Joined: 20-September 08

Re: Number Format Exception

Posted 19 February 2019 - 11:29 AM

With only a brief glance at your code, you're not checking for brackets (which will therefore be put forward for evaluation as an integer - which will obviously fail)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1