why isn't the input text file being read into the ArrayList<String>, diary?
Posted
by hmg
on Stack Overflow
See other posts from Stack Overflow
or by hmg
Published on 2010-06-17T04:54:12Z
Indexed on
2010/06/17
5:03 UTC
Read the original article
Hit count: 170
java
Here's my code:
public void readFile() throws IOException {
System.out.println("Input file name: ");
Scanner in = new Scanner(System.in);
String readName = in.nextLine();
Scanner reader = new Scanner(new File(readName));
System.out.println("Reading file...");
while (reader.hasNextLine()) {
System.out.println(reader.nextLine());
}
System.out.println("File read.");
Scanner inAgain = new Scanner(System.in);
System.out.println("Would you like to read this into the current Diary?");
System.out.println("Warning: This will overwrite your current Diary!");
String readFileConfirm = inAgain.nextLine();
ArrayList<String> readData = new ArrayList<String>();
if (readFileConfirm.equalsIgnoreCase("yes")) {
for (int i = 0; i < readData.size(); i++) {
readData.add(reader.nextLine());
}
System.out.println("Data added to extra array...");
System.out.println("Here are the contents.");
for (int i = 0; i < readData.size(); i++) {
System.out.println(readData.get(i));
}
System.out.println("Contents read.");
System.out.println("Now overwriting current Diary with read file...");
diary.clear();
for (int i = 0; i < diary.size(); i++) {
diary.add(readData.get(i));
}
System.out.println("New Diary created!");
System.out.println("Use 'recall' to see your new Diary!");
} else {
System.out.println("Very well, returning to first command entry.");
}
}
Thanks in advance!
-h
© Stack Overflow or respective owner