sorry if my formatting is garbage. I'm trying to add 2 strings and a boolean to an arrayList doing the following.
while((in.hasNextLine())){
list.add(new Task(in.next(),in.next(), in.hasNextBoolean()));
}
I keep getting a no such element exception. If I take the while loop away like so
list.add(new Task(in.next(),in.next(), in.hasNextBoolean()));
It functions fine but I can only add the first line of the text file. Is there something wrong in the loop or with the Scanner?
try {
in = new Scanner(f1);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
while((in.hasNextLine())){
list.add(new Task(in.next(),in.next(), in.hasNextBoolean()));
}
f1 is declared as File f1 = new File ("src/mylist.txt");
Any help would be great! Thanks!
in.next(),in.next(), in.hasNextBoolean()What do you think with this?hasNextBoolean()does not consume the boolean value. --- Also, after the lastnext()(ornextBoolean()) call, the "cursor" is still at the end of the line, so if your last line ends with a newline,hasNextLine()will return true, even though there are no more elements. --- UsingScannerto read lines of 3 values like that is very error-prone. Suggest usingBufferedReader,readLine()andsplit(), for more controlled parsing.