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!

Filling an array with integers in a text file

843785Jul 31 2008 — edited Jul 31 2008
I'm trying to develop a program that will read any text file that has a list of integers; and then places those values into an array. The arrays length equals the number of lines (each line consisting of one integer); and each index of the array holds the integer on its perspective line.

I've developed the following code; I know that it'll get the proper length of lines within a text file (I've tested that part)..
But after that something goes wrong in the for loop I think..
import java.io.*;
import java.util.*;

public class calculate
{
	public static void main(String[] args)
	{
		try
		{
		 System.out.println("Enter output file name:");
		 Scanner keyboard = new Scanner(System.in);
		 String fileName = keyboard.next();
		 BufferedReader inputStream = new BufferedReader(new FileReader(fileName));
			
		 int i;
		 int length = 0;
       	                 while (inputStream.readLine() != null)
                                 length++;
		 int[] array = new int[length];

		 for (i = 0; i < array.length; i++)
		 array[i] = inputStream.read();
			
		 int max = array[0];
		 int min = array[0];
		 for(i = 0; i < array.length; i++){
    		 if(array[i] > max) max = array;
if(array[i] < min) min = array[i];}

System.out.println("Largest = " + max);
System.out.println("Smallest = " + min);
}
catch(IOException e)
{
System.out.println("Error reading from file");
}

}
}
the text file can be any file name with any amount of numbers that perhaps looks like the following..

numbers.txt
23
14
36
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 28 2008
Added on Jul 31 2008
12 comments
363 views