Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading numbers from a text file

843785Jul 17 2008 — edited Jul 17 2008
Yesterday I wrote up a program where it reads in a txt file with a list of numbers, and from there it outputs the highest number, lowest number and average of all the numbers. My code works for the most part except for one snag. It will not read a 0 by itself in the txt file. The list in the text file is this:

10
50
100
70
200
1000
60
30
40
0

My program returns 1000 as the highest, but since it completely ignores zero, it lists ten as the lowest and gives me the average of nine numbers instead of ten. Whats even weirder is that if I change that zero to another number, my program outputs nothing. Any idea where I went wrong here? Any help would be appreciated.
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.math.*;

public class FileNumber
{
    public static void main(String[] args)
    {
	     int count = 0; 
		  double sum = 0; 
		  double max = 0; 
        double min = 0; 
        double next=0;
		  
		  
        System.out.print("Enter file name: ");
        Scanner keyboard = new Scanner(System.in);
        String fileName = keyboard.next();
        Scanner inputStream = null;
        System.out.println("The file " + fileName + " contains a list of numbers.");
						   
       try
       {
           inputStream = new Scanner(new File(fileName));

       }
		 catch(FileNotFoundException e)
       {
           System.out.println("Error opening the file " + fileName);
           System.exit(0);
       }
   
       while (inputStream.hasNextLine())
       {
           for(int i=0; next >= 0; i++)
                {
                 next = inputStream.nextDouble();
                        if(next > 0)
                        {
								
                                count += 1; //keeps track of every entry
                                sum += next; //adds up all the entries
              
                                if(next > max)//if the next entry is higher then the previous entry, the it become the new high entry
                                    {
                                    max = next;
                                    }
                                

                                if(i==0)
                                    {
                                    min = next;
                                    }
                                

                                if(next < min)//if the next entry is lower then the previous entry, the it become the new low entry
                                    {
                                    min = next;
                                    }
                          }
                			  else
            				  {
                   				double average = (sum/count); //finds the average of all the entries
					 					System.out.println("The highest number is " + max); // outputs the highest
                					System.out.println("The lowest number is " + min); // outputs the lowest
                					System.out.println("The average of all the numbers is " + average);  //outputs the average
										
    								}
		  				}
				}
				
       inputStream.close( );
       }
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2008
Added on Jul 17 2008
6 comments
1,198 views