-1
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

this error pops up:

Type an output file name: Exception in thread "main" java.util.NoSuchElementException
deadshot
  • 8,881
  • 4
  • 20
  • 39
  • post the complete error and how are you calling this function? – deadshot Aug 05 '20 at 03:35
  • 1
    Why you checking one scanner `while (input.hasNext())` and then querying another `String word = console.next();`? – talex Aug 05 '20 at 03:40
  • What is `aeiou(first)`? Do you have a `aeiou(char c)` method? – robinhoodjr Aug 05 '20 at 03:41
  • Type an output file name: Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1371) at CJMadLibs.cCommand(CJMadLibs.java:61) at CJMadLibs.commandPrompt(CJMadLibs.java:32) at CJMadLibs.main(CJMadLibs.java:15) @deadshot this is the full error, I am calling this method in another method, inside an if/else loop. The method call is cCommand(in). – CeeJay Goh Aug 05 '20 at 03:56
  • @robinhoodjr yes, there is another method called aeiou(char c). – CeeJay Goh Aug 05 '20 at 03:58
  • Since i am looking at a .txt file, its just a bunch of sentences. So what I am doing here is looking at each line, so loops at each line, then at each line, i look at each word. @talex – CeeJay Goh Aug 05 '20 at 04:00

1 Answers1

0

NoSuchElementException error is because either

String replace = in.next();

or

String word = console.next();

still calling next but one of them no longer has a next element to provide. Make sure to call hasNext() first before calling next().

Fahim Bagar
  • 798
  • 7
  • 17
  • I dont get how the String replace = in.next(); is an error, am I not just creating a string where the user types in something? Also, Im pretty sure my while loop already calls the hasNext() method. correct me if im wrong, but im still unsure what the problem is here. – CeeJay Goh Aug 05 '20 at 04:03
  • Yes, your loop has already calls `hasNext()` but it only for `Scanner input` variable. While the `Scanner console` and `String replace = in.next();` don't have the `hasNext()` safeguard. Unless you show update your question with the file and whatever comes in `Scanner in`, I can't trace it. – Fahim Bagar Aug 05 '20 at 04:42
  • Try to match every `hasNext()/hasNextLine()` with the safeguard `next()/nextLine()`, it should have been equal. – Fahim Bagar Aug 05 '20 at 04:43