Where to Place Try/Catch Statement in Postfix Expression Evaluation?

  • Thread starter Thread starter muna580
  • Start date Start date
  • Tags Tags
    Confusion
AI Thread Summary
The discussion revolves around evaluating a postfix expression using a stack in Java. The main focus is on the placement of try/catch statements for error handling in the evaluation method. It is suggested that the try/catch should encompass the Integer.parseInt() call to catch NumberFormatException for non-integer strings. Additionally, there is a consideration for handling malformed sequences, such as '2+', which could lead to an empty stack. The consensus is that malformed sequences will ultimately result in an empty stack since the stack only holds numbers during the evaluation process. The existing try/catch structure already covers most potential errors, but attention is drawn to the need for handling cases where the stack might be empty during the return statement.
muna580
I am evaluating a postfix expression using stacks and stuff. Below is the method I made. But I am not sure where to put the try/catch satement and what is it that i have to catch.

Code:
	public int evaluateRPN()
	{
		StringTokenizer token = StringTokenizer(postfix);
		stk.clear();
		
		try
		{
			while(token.hasMoreTokens())
			{
				String s = token.nextToken();
				
				if(isNumber(s))
					stk.push(new Integer(Integer.parseInt(s)));
				else
				{
					char operator = s.charAt(0);
					int x1 = ((Integer)stk.pop()).valueOf();
					int x2 = ((Integer)stk.pop()).valueOf();
					
					int x3; 
					
					switch(operator)
					{
						case StackConstants.ADDITION_OPERATOR:
							x3 = x2 + x1;
							stk.push(new Integer(x3));
						break; 
						
						case StackConstants.SUBTRACTION_OPERATOR:
							x3 = x2 - x1;
							stk.push(new Integer(x3));
						break; 
						
						case StackConstants.MULTIPLICATION_OPERATOR:
							x3 = x2 * x1;
							stk.push(new Integer(x3));
						break; 
						
						case StackConstants.DIVISION_OPERATOR:
							x3 = x2 / x1;
							stk.push(new Integer(x3));
						break; 
											
						case StackConstants.MODULUS_OPERATOR:
							x3 = x2 % x1;
							stk.push(new Integer(x3));
						break; 
					}
				}
			}
		}
		catch(EmptyStackException e)
		{
			
		}
		
		return ((Integer)stk.pop()).valueOf(); 
	}
 
Technology news on Phys.org
You should put the try/catch around the Integer.parseInt() call. It will throw a NumberFormatException if the string you are trying to parse into an integer is not an integer, like the string "abc."
 
I don't think so, that parseInt() is only when isNumber(). I think the try/catch should catch malformed sequences like '2+' (and I'm not saying where that is because I imagine this is homework).
 
Oh, silly me, there is already a try/catch around almost the entire thing. The only place left is the return statement (which will error on empty stack). Stupid question. Although, if you want to catch malformed sequences you can still do that.
 
Last edited:
Having thought about this more, I realize that malformed sequences will always result in an empty stack because the stack only ever contains numbers. I had in mind a stack containing the complete expression, whereas this one only contains intermediate results.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
9
Views
4K
Replies
16
Views
4K
Replies
1
Views
3K
Replies
7
Views
3K
Back
Top