File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes StringTokenizer only receiving 4 tokens from a list of 9 tokens Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "StringTokenizer only receiving 4 tokens from a list of 9 tokens" Watch "StringTokenizer only receiving 4 tokens from a list of 9 tokens" New topic
Author

StringTokenizer only receiving 4 tokens from a list of 9 tokens

Andrew Alan Smith
Greenhorn

Joined: Nov 21, 2012
Posts: 3
Hi everyone, thank you for taking the time to read my post and I was hoping somebody could be so kind as to point out what exactly is going wrong in my code.
I'm trying to write a program for a Java course that gets a list of innings scores from the user for one team, then a list from the other team and sums them up and declares a winner.
I ask the user to input the scores in a list delimited by ',' e.g. 1,2,3,4,5,6,7,8,9 but once the list is tokenized it has only 4 tokens inside. I've been scratching my head all morning on this problem.

Edit to add: If I enter 1,2,3,4,5,6,7,8,9 I get the following output:

Enter a list of 9 inning scores for Team 1, e.g. 1,2,3,4,5,6,7,8,9: 1,2,3,4,5,6,7,8,9
4
Enter a list of 9 inning scores for Team 2, e.g. 1,2,3,4,5,6,7,8,9: 1,2,3,4,5,6,7,8,9
4
The final score was tied at 15.


Please see the following code and thank you in advance for your help,
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Welcome to the Ranch
Note we have added code tags and broken your line 75, because long lines are difficult to read. See how much better it looks. You ought to break some of the other lines similarly.

Why are you using StringTokenizer in the first place? It has been officially classified as legacy code for 11 years.
You must be getting 9 elements, otherwise your program would terminate.
There are all sorts of things I would recommend to change, particularly the repeated use of the number 9. You should factor that out as a constant: private static final int INNINGS_IN_GAME = 9; You should use the token count instead of the number to set the size of the array. Using numbers directly is risky; you can set the array to the wrong size by mistake.
Your problem appears to be in the method which changes the text into numbers. I have changed that, and got rid of the null test. Letting nulls into your code is dangerous and should be avoided as much as possible.Then I tried it out
[campbell@computer java]$ java Q13
Innings Winner
Andrew Smith 2012
This program will determine the winner of a game of baseball over 9 innings.
-------------------

Enter a list of 9 inning scores for Team 1, e.g. 1,2,3,4,5,6,7,8,9: 8,2,4,1,2,8,5,6,1
[deleted]
Enter a list of 9 inning scores for Team 2, e.g. 1,2,3,4,5,6,7,8,9: 7,2,4,1,9,7,5,3,0
[deleted]
Team 2 won the game with a score of 17 over 9 innings. The losing team scored 23.
Try it, in that format and see what happens. Try changing the for loops to while(myTokenizer.hasNextToken())... and check here I have given you the correct method name.

What is happening is that you are incrementing i, until it meets the token count. But the token count is not 9. The token count is 4, because you have removed 5 tokens. The last 4 token are never read.

My recommendation:-
Get rid of the tokenizer altogether. Use the nextInt() method of java.util.Scanner, with "[\\s,]+" as its delimiter. That means a combination of commas and whitespace (escape-s for space), and + means any number > 0.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
The reason the losing team scored more than the winning team is that you are printing score1 before score2 regardless.
I have checked, and I did give you the wrong method name.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7526
    
  18

Andrew Alan Smith wrote:Hi everyone, thank you for taking the time to read my post and I was hoping somebody could be so kind as to point out what exactly is going wrong in my code.

Well, Campbell's basically given you the answer, but here are a few general tips for you (apart from not using StringTokenizer):
1. If a variable has an assignable value, assign it. You've managed to create two variables in two different ways in 3 lines, viz:why not just:?

2. Avoid using the result of a function in a loop test. As Campbell pointed out, this is actually your problem, but it's also a good general rule to follow. It's also worth remembering that you can initialize more than one variable in a for loop, so you could have written yours:however, since you've already calculated the value anyway on line 6, why not just store it?

3. Don't repeat code. Your two summing loops starting at line 61 do exactly the same thing, so I'd put them in a method, eg:
public int sum(int[] array) { ...
I leave it to you to work out the details; but don't forget that the sum of a bunch of int's might be too large to fit into an int. How you choose to handle that is up to you.

4. (and possibly most important) When you run into problems: StopCoding (←click). You will never solve them by simply bashing away at a keyboard.

HIH

Winston


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

Joined: Mar 17, 2011
Posts: 7526
    
  18

Winston Gutkowski wrote:but here are a few general tips for you...

Oh, and another one is not to return null. Far better (in your case) to return an empty array, viz:
Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Winston Gutkowski wrote: . . . Well, Campbell's basically given you the answer, . . .
I know we usually avoid giving straight answers, but I thought it would take you ages to work out the problem. It took me long enough, and it wasn’t until I ran my code and saw those 0s that I had any idea what was happening. There are some solutions which beginners will never see, unless they are shown them.
You probably don’t know how to throw Exceptions yet, but that is an alternative to returning the 0‑length array.
More useful advice from Winston, as usual.

And “You’re welcome.
Andrew Alan Smith
Greenhorn

Joined: Nov 21, 2012
Posts: 3
Thanks so much for all of the help and insight! My professor had actually mentioned using StringTokenizer as a possible way to break the score list up - I'll have to let him know its deprecated code.

I still don't really understand why the tokenizer was only receiving 4 of the tokens and dropping the other 5 but I suppose there isn't much of a point to trying to figure out a deprecated class anyhow. I've tried to incorporate all of the insights and changes that you've all provided me with, and it works like a beauty!

Thanks again I really appreciate the wisdom living at this ranch!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
Tokenizer is not actually deprecated. It is legacy code.
The tokenizer was not receiving 4 tokens. It was losing 1 token every time your loop ran, and when your loop terminated, 5 had been used and 4 remained. You will have to go through your original loop with a pencil and paper, like this:-I shall let you finish it. There may be a little error in my counting. I don’t like giving too much away and I am confident you can work out the rest of the loop yourself.
There are all sorts of style things in your code which could be improved.
I think you are obliged to acknowledge any help you receievd here, otherwise you could get into trouble.
You are still printing score1 before score2 regardless.

And … you’re welcome
Andrew Alan Smith
Greenhorn

Joined: Nov 21, 2012
Posts: 3
Oh shoot.. I see what you mean about team1Score always appearing first in the formatted string...
I will definitely be including in a "programming journal" I have to write as I complete these programs that I asked and received help from this forum. In fact I've already told a few class mates about the help I received here as well

As for my coding style I'm sure there are lots of things I could improve, and I would love to implement any changes - could you recommend anywhere that I could read more info please?

Thank you again

Edit to add:
This is the solution I came up with to display the proper winning score in the final formatted string:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37950
    
  22
You can download code style tools; can’t remember any names at the moment. I can think of at least two places where you can find about style. I don’t think either finds general acceptance; I actually tend to use a combination of the two. I greatly prefer the indentation conventions in No 1.
Link 1 Link 2

That is a good way to get the scores printed in the right order. If you are using printf you can actually incorporate the ?: expressions inside the arguments to printf.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: StringTokenizer only receiving 4 tokens from a list of 9 tokens
 
Similar Threads
help with assignment
trying to find union/intersection of arrays containing integers
question about using boolean to check a condition of an object?
Netbeans and Ctrl + z (end of file)
How to populate array with user input