Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Handling of tokens

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi folks,
I have written an application that acts like a simple calculator and performs addition or subtraction of numbers entered in a certain pattern. I was successful in writing the program and compiling it and exceuting it. I am having little bit of trouble in handling exceptions.
I have to handle four kinds of errors and i did get 1 and 3. But didnt know how to achieve 2 and 4.
1. No input at all. The user presses �calculate� and the input text field has nothing to calculate. Put the message, �Enter An Expression� in the top text field if this occurs (and make the bottom text field blank.
2. Wrong number of operators and operands. There should always be an odd number of items (�55� is one instance, � 3 + 2� is three, etc.) Report �Wrong Number Of Elements� in the bottom text field if there is not an odd number of elements (after you tokenize on �+� and �-� ).
3. Invalid operand. If you find something that is not a valid operand (i.e., not an integer) in a place where you would expect an operand to be, then leave the user�s expression in the top text field and put the message �Invalid Operand Or Operator� in the bottom text field.
4. Invalid operator. If you find something that is not a valid operator (i.e., not a �+� or a �-�) in a place where you would expect to find an operator, then leave the user�s expression in the top text field and put the message �Invalid Operand Or Operator� in the bottom text field.
Below is my code:

My code handles the follwing exceptions.
When the user fails to enter any value and presses calculate, A message "Enter an Expression" appears in the input text field. if the user enters say "12 - a" i display a message invalid operand.
But my code does not catch an exception like say "12 & 2" then i should display invalid operator and also a message wrong number of elements if the user enters "12 - ". Any help would be greatly appreciated.
Thank you,
kt
[ July 30, 2005: Message edited by: kanaka tam ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic