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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

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