File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
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 two of them. But didnt know how to achieve the other two.
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.
D. 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:


Thank you,
kt

[ July 29, 2005: Message edited by: kanaka tam ]
[ July 29, 2005: Message edited by: kanaka tam ]
David C. Meyers
Greenhorn

Joined: May 20, 2004
Posts: 14
I'm sure there are many ways to handle this, but here is one suggestion. In your calcStrValue method, use a boolean to keep track of if your token is even or odd. If it is odd check to see if it is a number. If it is even, and it is not +/- throw an exception of your own design and catch it from the caller then display an error message.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

For some reason, continued with a slightly reworded repost here.


[Jess in Action][AskingGoodQuestions]
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Originally posted by Ernest Friedman-Hill:
For some reason, continued with a slightly reworded repost here.


Sorry about that, didn't mean to do that on purpose. Totally forgot about posting i did yesterday from school and posted it again. Anyway, i have completed 75% of the problem.
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
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Hello folks,
The application below is supposed to catch four kinds of exceptions and i have tried my best with a little help from a fellow memeber but not very successful in catching all of the exceptions. I am very exhausted and I can't figure out what is that I am doing wrong? If anyone could help me i would greatly appreciate that.


thanks,
kt
[ August 01, 2005: Message edited by: kanaka tam ]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Can you please give us some more detail? What do you expect this to do? What does it ACTUALLY do? I'm not at my own computer at the moment, so I can't compile your code to see for myself. However, I'll be glad to help you if you provide some more information.

Layne


Java API Documentation
The Java Tutorial
 
 
subject: Exception Handling
 
Similar Threads
problems the program
Uncomprehendable error messages
Calculator clear button not working...
Exception Handling of tokens
Multiple Windows in Applets