Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# String Tokenizer

Angela Hill
Greenhorn
Posts: 19
Hi I am trying to figure out why this code is not working! I have spent about 2 or 3 hours trying to figure it out and after numerous amount of research I still can't figure it out please help!!! Thank you so much for any help provided!

package edu.umsl.mis3806;

import java.io.*;
import edu.umsl.mis3806.KbdInput;
import java.util.*;

{

public static void main(String [] args) throws IOException
{

StringTokenizer st= new StringTokenizer(line);
int input, sumA, sumB, sumC, sumD, sumF;
sumA=sumB=sumC=sumD=sumF=0;
//String s = st.nextToken();

KbdInput kbd = new KbdInput();

while (st.hasMoreTokens())
{

if(input==-99) System.exit(1);

while(input != -99)
{

if(input>=90){
sumA++;
System.out.println(new Integer(input) + " A");
}
else if(input >= 70){
sumB++;
System.out.println(new Integer(input) + " B");
}
else if(input >= 50){
sumC++;
System.out.println(new Integer(input) + " C");
}
else if(input >= 35){
sumD++;
System.out.println(new Integer(input) + " D");
}
else{
sumF++;
System.out.println(new Integer(input) + " F");
}

}
// System.out.println(st.nextToken());
System.out.println("The total number of A's is " + new Integer(sumA ));
System.out.println("The total number of B's is " + new Integer(sumB ));
System.out.println("The total number of C's is " + new Integer(sumC ));
System.out.println("The total number of D's is " + new Integer(sumD ));
System.out.println("The total number of F's is " + new Integer(sumF ));
}
}
}

Nicola Garofalo
Ranch Hand
Posts: 308
What is the problem?
It does not compile, or you are getting some runtime errors?

Angela Hill
Greenhorn
Posts: 19
Sorry I'm new if you can't tell these are the errors that I get when I run the program. I am using NetBeans. Hope that helps

Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)

Nicola Garofalo
Ranch Hand
Posts: 308
First of all.
We should know what KbdInput class does, in particular one of its methods, readInt().
it does not return an int as expected but an empty String

Second:

You create a StringTokenizer of the String taken as input, i.e. String line

But you don't go through its tokens!!!
So the loop

will probably never end

Angela Hill
Greenhorn
Posts: 19

Nicola Garofalo
Ranch Hand
Posts: 308
It reads a line and it assumes this line will be an integer.

You could delete it and remove all the instructions

A nice method in KbdInput is readString() that returns a String that you can tokenize

Assuming that a user will enter values 60 70 40 30 -99 (but you should check user input token by token)

method readString() will return the String "60 70 40 30 -99"

You can tokenize that String with StringTokenizer

int variable input could become, for example

the inner loop

it's useless and you should remove it.

Start to make this changes and see what's happening

Nicola Garofalo
Ranch Hand
Posts: 308
Last thing.

Remove lines

Angela Hill
Greenhorn
Posts: 19
ok now it is only reading one number out of the series. this is what I have for my code now.

Nicola Garofalo
Ranch Hand
Posts: 308
You did not make all the changes i asked you to make.

1) Remove from line 14 to line 16
2) In line 18 you must read the String returned by method KbdInput.readString()

3) Remove line 19, or you will miss some tokens

Angela Hill
Greenhorn
Posts: 19
I get a runtime error after removing these lines

Nicola Garofalo
Ranch Hand
Posts: 308
No, you won't

Did you make all the changes i told you ?

Angela Hill
Greenhorn
Posts: 19
Yes sorry I replied before I saw your other messages...However, I can only get it to read the first number in the series rather than them all.

Nicola Garofalo
Ranch Hand
Posts: 308
Could you please post the code as it is now?

Angela Hill
Greenhorn
Posts: 19
Here ya go...Sorry to be such a pain

Nicola Garofalo
Ranch Hand
Posts: 308
Line 23 was a while and now it's an if. Put the while back

Angela Hill
Greenhorn
Posts: 19
AWESOME!!! Thank you so very much!

Nicola Garofalo
Ranch Hand
Posts: 308

you are welcome

Winston Gutkowski
Bartender
Posts: 10527
64
Angela Hill wrote:AWESOME!!! Thank you so very much!

It might be worth mentioning that StringTokenizer is not recommended any more. Specifically, the docs say:
"StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."

However, for your type of use, there is also java.util.Scanner.

Winston

Campbell Ritchie
Sheriff
Posts: 50702
83
Why are you creating Integer objects to display the sums? You can use the sumA, sumB etc. values directly.

Campbell Ritchie
Sheriff
Posts: 50702
83
Winston Gutkowski wrote: . . . It might be worth mentioning that StringTokenizer is not recommended any more. . . .
Why do so many people seem to teach the use of tokenizers? There may be some old books out there, but the newest API I found which doesn’t call Tokenizer legacy was 1.3. So it would appear to have been “legacy” for over ten years, and we still see so many questions about it.