Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trials and tribulations, trys and catches

 
DANIEL GRESHAM
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it doing? What errors are you seeing?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
DANIEL GRESHAM
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 341
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your fetch() method, you should keep inputData.nextToken() into a String variable.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic