Моя программа компилируется, но по-прежнему показывает ошибку java .util.NoSuchElementException - PullRequest
0 голосов
/ 05 августа 2020
public static void cCommand(Scanner in) throws FileNotFoundException {   

      System.out.println();      

      System.out.print("Type an output file name: "); 
      String outFile = in.nextLine();
      
      PrintStream ps = new PrintStream(new File("out.txt"));
      Scanner input = new Scanner(new File("story.txt"));   
      
      while (input.hasNextLine()) {
         String line = input.nextLine();
         Scanner console = new Scanner(line);
         while (input.hasNext()) { 
            String word = console.next();
            if (word.startsWith("<") && word.endsWith(">")) { 
               char first = word.charAt(1);
               String a = aeiou(first);
               word = word.replace("<"," ");
               word = word.replace(">"," ");
               word = word.replace("-"," ");
               System.out.print("Please type" + a + word + ": ");
               String replace = in.next();
               ps.print(" " + replace);
            } else {
               ps.print(" " + word);
            }
         }
      }
      
   } //end of cCommand method

эта ошибка выскакивает:

Type an output file name: Exception in thread "main" java.util.NoSuchElementException

1 Ответ

0 голосов
/ 05 августа 2020
Ошибка

NoSuchElementException вызвана тем, что

String replace = in.next();

или

String word = console.next();

по-прежнему звонит next, но один из них больше не нужно предоставлять следующий элемент. Обязательно позвоните по номеру hasNext(), прежде чем звонить по номеру next().

...