# Tokenizing? Help writing a static method that accepts a string of text as a parameter

• 10-06-2013, 09:52 PM
manibby93
Tokenizing? Help writing a static method that accepts a string of text as a parameter
Write a static method named numberStatistics that accepts a string of text as a parameter. Assume that the text is read from a file, "score.txt." The text is a series of strings (name) and integers (score). The method should process this text (maybe multiple lines) and report various statistics about the integers. Report the total number of the numbers, the sum of the numbers, the average of the numbers and the minimum and maximum numbers. For example, if the text is the following:

Helene 5 Jordan 7 Eve 2 Ken 8 Andrew 9
David 10 Amy 12 Sean 98 Patrick 7
Joy 14 Sue 20 Jimmy 22

Your method should produce the following output.

Total numbers = 12
Sum of numbers = 214
Average score = 17.833
minimum number = 2
maximim number = 98

This is what I got, but I get Static Error: This class does not have a static void main method accepting String[]. I KNOW what that means, but then how do I use a boolean?

Code:

```import java.util.*;  // for Scanner public class negativeSum { public static boolean main(String line) {  Scanner lineScan = new Scanner(line);  int sum = 0;  int count = 0;  while (lineScan.hasNextInt()) {  int next = lineScan.nextInt();  sum += next;  count++;  if (sum < 0) {  System.out.println(sum + " after " + count + " steps");  return true;  }  }  System.out.println("no negative sum");  return false; // not found } }```
• 10-06-2013, 10:13 PM
jim829
Re: Tokenizing? Help writing a static method that accepts a string of text as a param
The static main method is used as the entry point into the program. Why not simply construct a separate method within the class to do your processing? You can then report success or failure from the method to the caller. Also, by convention, class names should start with capital letters.

Regards,
Jim
• 10-06-2013, 10:29 PM
manibby93
Re: Tokenizing? Help writing a static method that accepts a string of text as a param
Okay. I quit, thanks.
• 10-07-2013, 12:45 AM
jim829
Re: Tokenizing? Help writing a static method that accepts a string of text as a param
Quote:

Originally Posted by manibby93
I quit

Huh? What gives?

Regards,
Jim
• 10-07-2013, 01:41 AM
jim829
Re: Tokenizing? Help writing a static method that accepts a string of text as a param
First, you said the input is

name number name number etc.

So you need to check if any input is available as you while condition. Then, assuming your data is consistent, you need to read
in the name and then the number.

And you need to move your print statements outside of you while loop.

Finally, when finding max and min values, you need to initialize them properly. Different circumstances allow
for different methods. In your case, this means that min must set to be larger
than any value in your data and max must be set to a value that is smaller than your data.

Regards,
Jim
• 10-07-2013, 02:45 AM
manibby93
Re: Tokenizing? Help writing a static method that accepts a string of text as a param
I'm not allowed to duplicate a thread, but if I'm in a rush, am I just supposed to wait? Like, how in the world does that make sense?