Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Tokenizer

 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the problem?
It does not compile, or you are getting some runtime errors?
 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a runtime error after removing these lines

 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you won't

Did you make all the changes i told you ?
 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post the code as it is now?
 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here ya go...Sorry to be such a pain

 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 23 was a while and now it's an if. Put the while back

 
Angela Hill
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AWESOME!!! Thank you so very much!
 
Nicola Garofalo
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


you are welcome
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50236
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you creating Integer objects to display the sums? You can use the sumA, sumB etc. values directly.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50236
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic