Reputation: 115
For some reason when I try scan a .txt file, it is failing to find any lines and thus causing the error:
java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
Code:
File file = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
Scanner scanner = new Scanner(new FileInputStream (file), "UTF-8");
while(scanner.hasNextLine()){
modsList.add(scanner.nextLine());
System.out.println(scanner.nextLine());
}
I do not close the scanner. modsList is a List that I try add each line to so I can check if a mod exists in chat (user) matches one in the list, however the error is on System.out...
I checked the .txt file exists with the following:
File f = new File("C:\\Users\\kayc0\\Desktop\\CkayBotBets\\mods.txt");
if(f.exists() && !f.isDirectory()) {
System.out.println("file exists");
}
Anyone any idea why the lines are not being read?
.txt contents:
abkayckay
kayc01
Thanks, any help appreciated.
Upvotes: 3
Views: 1949
Reputation: 69440
You call nextLine
tice. Change to:
while(scanner.hasNextLine()){
String value = scanner.nextLine()
modsList.add(value);
System.out.println(value);
}
Upvotes: 3
Reputation: 201399
You are currently reading two lines instead of one, save the line you read to add to your list and display with the same line.
while(scanner.hasNextLine()){
String line = scanner.nextLine();
modsList.add(line);
System.out.println(line);
}
Upvotes: 5