• Post Reply 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

Trials and tribulations, trys and catches

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is it doing? What errors are you seeing?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your fetch() method, you should keep inputData.nextToken() into a String variable.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic