Results 1 to 6 of 6
  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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?

    Java 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 
    } 
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default Re: Tokenizing? Help writing a static method that accepts a string of text as a param

    Okay. I quit, thanks.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default Re: Tokenizing? Help writing a static method that accepts a string of text as a param

    Quote Originally Posted by manibby93 View Post
    I quit
    Huh? What gives?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,384
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    manibby93 is offline Member
    Join Date
    Sep 2013
    Posts
    77
    Rep Power
    0

    Default 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?

Similar Threads

  1. Replies: 3
    Last Post: 10-04-2013, 10:49 PM
  2. Replies: 7
    Last Post: 04-28-2012, 02:10 AM
  3. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 08:19 AM
  4. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 04:44 PM
  5. Replies: 2
    Last Post: 10-15-2010, 05:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •