Wednesday, October 24, 2007

FOr Fizkiz

There you go fizkiz. To find the average, smallest and largest and the total sum of the text file.


package cruft;
import java.io.*;
import java.util.*;
public class NumberSorter
{
public static void main(String[] args)
{
String fileName;
int count=1;
int sum=0;
int value;
int largest =0;
int smallest = 0;
double average =0;
System.out.println("I will sort through the numbers and give you the ");
System.out.println("smallest, largest, and the average of all the numbers.");
System.out.println("Enter the file name:");
Scanner s = new Scanner(System.in);
try
{
System.out.print("File Name: ");
fileName = s.next().trim();
Scanner scan = new Scanner(new File(fileName));
while(count<=50) {
value = scan.nextInt();
if(value > largest)
largest = value;
if(value < smallest)
smallest = value;
sum += value;
count++;
}
average = sum/50;
}
catch(FileNotFoundException e)
{
System.out.println("File not found.");
}
catch(IOException e)
{
System.out.println("Error reading from file");
}
System.out.println("Sum: " + sum);
System.out.println("Highest: " + largest);
System.out.println("Smallest: " + smallest);
System.out.println("Average: " + average);
}
}

4 comments:

Unknown said...

thank you guys, this program is awesome now i understand what i needed to do.

Anonymous said...

No Worries fizkiz. Have fun :--)

PRAVINTH G said...

Hi Sule (chris) Good...
use p tag to keep the space before lines...
Keep this spirit...
Hi fizkiz, congrats ..you have learnt alot...

Anonymous said...

Yes master :--) I got you.