- #1
Robben
- 166
- 2
Homework Statement
How do I read in a file line by line into an array without using arraylist?
Homework Equations
None
The Attempt at a Solution
I know how to do this using BufferedReader, but I am wondering how to do this using Scanner? When I used BufferedReader I noticed that there must be two exceptions to be caught which were IOException and FileNotFoundException. Whereas a Scanner needs only a FileNotFoundException, why is that?
Java:
public class practice {
public String[] array;
Scanner inputStream = null;
Scanner n = new Scanner(System.in);
public String line;
public practice(String theFile) {
array = new String[150];
try {
inputStream = new Scanner(new FileInputStream(theFile));
line = inputStream.nextLine();
while (inputStream.hasNextLine()) {
for (int i = 1; i < array.length; i++){
array[0] = line;
//dont know what to put here
}
}
} catch(FileNotFoundException e) {
System.out.println(e.getMessage());
}
inputStream.close();
}
}
Last edited: