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
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.
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
author and iconoclast
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.