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