I'm trying to use scanner.nextLine() inside a loop, but I get an exception.
The problem is located in this part of the code.
while(!sentence.equals("quit")){
dealWithSentence(sentence, voc);
System.out.println("Enter your sentence:");
sentence = scanner.nextLine();
}
There is the exception:
Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source) at il.ac.tau.cs.sw1.ex4.SpellingCorrector.main(SpellingCorrector.java:34)
That's my full method code:
public static void main(String[] args) throws Exception{
Scanner scanner = new Scanner(System.in);
String filePath = scanner.nextLine();
if (filePath.contains(" ")){
scanner.close();
throw new Exception("[ERROR] the file path isnt correct");
}
File file = new File(filePath);
String[] voc = scanVocabulary(new Scanner(file));
if (voc == null)
{
scanner.close();
throw new Exception("[ERROR] the file isnt working");
}
System.out.println("Read " + voc.length + " words from " + file.getName());
System.out.println("Enter your sentence:");
String sentence = scanner.nextLine();
while(!sentence.equals("quit")){
dealWithSentence(sentence, voc);
System.out.println("Enter your sentence:");
sentence = scanner.nextLine();
}
scanner.close();