Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Norm

Thread: Tokenizing? It compiles, but I'm worried about "min" & "max"

  1. #1
    manibby93 is offline Member
    Join Date
    Sep 2013
    Rep Power

    Default Tokenizing? It compiles, but I'm worried about "min" & "max"

    The assignment is to:
    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.

    This is what I put together after the professor gave us a head start.

    Java Code:
    import*;     // for File 
    import java.util.*;   // for Scanner
    public class TEST {
      public static void main(String[] args) throws FileNotFoundException {
            Scanner lineScan = new Scanner(new File("score.txt"));        
     int numNums = 0; 
     int sum = 0; 
     int max = 0;
     int min = 0;
     while (lineScan.hasNextInt()) { 
     int number = lineScan.nextInt(); 
     sum += number; 
     if (number % 2 == 0) { 
     System.out.println("Total numbers = " + numNums); 
     System.out.println("Sum of numbers = " + sum); 
     System.out.println("Average  = " + (sum/numNums)); 
     System.out.println("maximum  = " + max);
     System.out.println("minimum = " + min); 
    It compiles, but I feel like I'm supposed to do something else for "max" & "min." I remember learning something about Math.min & Math.max. It compiling is enough for ME because I've been getting frustrated, but I do need to make sure...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: Tokenizing? It compiles, but I'm worried about "min" & "max"

    Normally the min number would be the smallest number and max the largest. To find which number is the min or max number requires comparing all the numbers as they are read using an if statement.
    One technique for doing the search is to initialize the min variable with the largest possible number before comparing it and the max variable with the smallest possible number. Then when the first compare is made the number will be smaller than the largest and larger than the smallest.
    Another technique is to use the first number that is read in as the initial value for the min and max variables. Then as compares are made the correct choice is made.

    BTW The statements should be indented to show nesting logic. Too many statements start in the first column.
    Last edited by Norm; 10-05-2013 at 01:06 AM.
    jim829 likes this.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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