aspose file tools*
The moose likes Beginning Java and the fly likes Trials and tribulations, trys and catches Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trials and tribulations, trys and catches" Watch "Trials and tribulations, trys and catches" New topic
Author

Trials and tribulations, trys and catches

DANIEL GRESHAM
Greenhorn

Joined: Feb 09, 2005
Posts: 21
I am using a try and catch for the first time, according to what my professor has gone over, this "in theory" should work, but it doesnt(big surprise). can some one show me how to write this code correctly , and explain to me why it is not currently working.
heres the code everything has been initialized and constructed

if you need to see all of the code to see if I have screwed up majorly, its posted under my earlier post from today, or just ask and ill post it again
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
What is it doing? What errors are you seeing?


James Carman, President<br />Carman Consulting, Inc.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

This isn't too bad. I see four problems, though:

First, the index tokE and and counter count1 are updated whether or not there was an error. Depending on what they are intended to represent, you may want to update them only if a token was parsed successfully. By the way, why do you need both of these? Wouldn't one suffice?

Second, the use of member variables in inappropriate. Everything shouldn't be a member variable -- only those things that represent the state of the object, or at least only those that really need their values shared between methods. In this code, everything is a member, when many of the variables could and should be local. Using local variables would make understanding and debugging the code easier, because it's easier to be sure of what's happening while looking at only a small section of code at once.

Third, perhaps the biggest problem, is that the error message in the JOptionPane will report not the token that caused the error, but the following one, which will then be skipped. This is because in both the try and the catch, you ask the tokenizer for another token, but you really want both these blocks to be referring to the same token. Store the token in a String variable (use a local!) then enter the try block and try to parse that variable. In the catch block, refer to the variable, not to the tokenizer. Now you'll get appropriate error reports and no more skipped tokens.

And finally, the size of the array tokenizedEntry is set before the tokenizer is even created, so you don't know how many tokens will need to be stored. You may be getting ArrayIndexOutOfBoundsExceptions if there are too many tokens to fit in the array. You could use the countTokens() method of the tokenizer to set the size of the array if you delay creating the array object until after the tokenizer is constructed.


[Jess in Action][AskingGoodQuestions]
DANIEL GRESHAM
Greenhorn

Joined: Feb 09, 2005
Posts: 21
exception in thread "awt-EventQueu-0"java.util.NoSuchElementException
at java.util.stringtokenizer.nexttoken<unknown source>
at NewTextTrial.fetch<NewTextTrail.java:90>
at NewTextTrial.actionPerformed<NewTextTrail.java:90>
at javax.swing.abstractButton.fireActionPerformed<unknown source>
at javax.swing.abstractButton$button$Handler.actionPerformed<unknown source>
at javax.swing.defaultButtonModel.fireActionPerformed<unknown source>
at javax.swing.defaultButtonModel.setPressed<unknown source>
at javax.swing.plaf.basicbuttonListener.mouseReleased<unknown source>
just to start with, theres at least 10 more runtime errors
just incase you need to look at the whole thing, here it is
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

The java.util.NoSuchElementException is telling you that you called nextToken() on the StringTokenizer, but there were no more tokens. This is due to my point number 3: you loop on while(hasMoreTokens()), but then you sometimes (when there's an error) call nextToken() twice in one loop iteration; that second call may come when there are no more tokens left.
Surasak Leenapongpanit
Ranch Hand

Joined: May 10, 2002
Posts: 341
In your fetch() method, you should keep inputData.nextToken() into a String variable.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Trials and tribulations, trys and catches
 
Similar Threads
StringTokenizer Class
to and fro out of a loop to a method in back?
Oh, God I NEED Help!!!!!!!!
getting NullPointerException
J3D NoClassDefFound