Results 1 to 2 of 2
 10042013, 11:46 PM #1Member
 Join Date
 Sep 2013
 Posts
 77
 Rep Power
 0
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 java.io.*; // 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(); numNums++; 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); } }
 10052013, 01:03 AM #2
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; 10052013 at 01:06 AM.
If you don't understand my response, don't ignore it, ask a question.
Similar Threads

access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12072012, 09:29 AM 
Convert string operation symbols "+", "", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10302012, 04:06 PM 
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02012012, 09:37 PM 
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01242009, 07:56 PM 
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10202008, 07:35 AM
Bookmarks