import java.util.*;
import java.io.*;
public class TreeTraversal{
//gives the length of the current file.
public static int setArrayLength() throws Exception{
FileReader file = new FileReader("numbers.txt");
LineNumberReader ln = new LineNumberReader(file);
int count = 0;
while (ln.readLine() != null){
count++;
}
System.out.println(count);
return count;
}
public static void createArray(int[] array) throws Exception{
Scanner file = new Scanner("numbers.txt");
for(int i = 0; i < array.length; i++){
array[i] = file.nextInt();
System.out.println(array[i]);
}
}
public static void main(String args[]) throws Exception {
//sets the length of the array using the setArrayLength() method above.
int[] array = new int[setArrayLength()];
//puts the integers of the file into the array, each line at a time.
createArray(array);
}
}
the file looks something like this:
12
23
53
11
9
28
25
etc..
and the length of the file is about 1000 lines.

New Topic/Question
Reply




MultiQuote



|