It's not a secret anymore!*
The moose likes Beginning Java and the fly likes String Tokenizer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String Tokenizer" Watch "String Tokenizer" New topic
Author

String Tokenizer

Angela Hill
Greenhorn

Joined: Jun 13, 2012
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 class Grades
{

public static void main(String [] args) throws IOException
{
System.out.println("Please enter grades or -99 to quit:");

BufferedReader br;
br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();

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();
input = KbdInput.readInt();

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");
}

input = KbdInput.readInt();
}
// 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

Joined: Apr 10, 2010
Posts: 308
What is the problem?
It does not compile, or you are getting some runtime errors?

Bye,
Nicola
Angela Hill
Greenhorn

Joined: Jun 13, 2012
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)
at edu.umsl.mis3806.KbdInput.readInt(KbdInput.java:11)
at edu.umsl.mis3806.Grades.main(Grades.java:25)
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
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

Can you post KbdInput.readInt() method?
Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
KbdInput.readInt() method could lead you to a runtime exception
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

Joined: Apr 10, 2010
Posts: 308
Last thing.

Remove lines



in your main method of class Grades, because method readString() does it too, and you would end up to read two lines
Angela Hill
Greenhorn

Joined: Jun 13, 2012
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

Joined: Apr 10, 2010
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

Joined: Jun 13, 2012
Posts: 19
I get a runtime error after removing these lines

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
No, you won't

Did you make all the changes i told you ?
Angela Hill
Greenhorn

Joined: Jun 13, 2012
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

Joined: Apr 10, 2010
Posts: 308
Could you please post the code as it is now?
Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
Here ya go...Sorry to be such a pain

Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
Line 23 was a while and now it's an if. Put the while back

Angela Hill
Greenhorn

Joined: Jun 13, 2012
Posts: 19
AWESOME!!! Thank you so very much!
Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308


you are welcome
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7696
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
Why are you creating Integer objects to display the sums? You can use the sumA, sumB etc. values directly.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38466
    
  23
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Tokenizer