-2

SOLVED: The issue was that I had another scanner set to System.in earlier in my code, thank you to @Abra for mentioning that it could be the cause of the issue!

while (Objects.equals(cycle, "yes")) {
            try (Scanner input = new Scanner(System.in)) {
                System.out.println("\nWould you like to gather more information? (y/n):");
                String answer = input.nextLine();
                if (Objects.equals(answer, "y")) {
                    menu();
                } else if (Objects.equals(answer, "n")) {
                    System.out.println("Have a nice day!");
                    cycle = "no";
                } else {
                    System.out.println(
                            "Sorry the respone entered can not be read. Please try again using either the letter 'y' or 'n'.");
                }
            }
        }

The above code is outputting the following error no matter what I change:

Would you like to gather more information? (y/n):
Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
        at Graph.main(Graph.java:246)

I can't figure out why this is happening my scanner works in other parts of the same code however this one specific area won't work. Any help is appreciated.

EDITED CODE:

        while (Objects.equals(cycle, "yes")) {
            System.out.println("\nWould you like to gather more information? (y/n):");
            String answer = input.nextLine();
            if (Objects.equals(answer, "y")) {
                menu();
            } else if (Objects.equals(answer, "n")) {
                System.out.println("Have a nice day!");
                cycle = "no";
            } else {
                System.out.println(
                        "Sorry, the response entered can not be read. Please try again using either the letter 'y' or 'n'.");
            }
        }

The scanner is now at the top of this file not near this specific area of code, it reads:

    static Scanner input = new Scanner(System.in);
  • 1
    Hello and welcome. Please ask one question per post. See [ask]. – Federico klez Culloca May 23 '22 at 08:30
  • @FedericoklezCulloca sorry didn't know about that rule! It won't allow me to create a second post yet but as soon as I am able I will split them – user17395771 May 23 '22 at 08:32
  • *code is intended to delete a username from a .txt file* - not even close. – Scary Wombat May 23 '22 at 08:36
  • 1
    By the way, in your second question, if that's all you're doing to delete a line from a file, solving the immediate syntax error won't give you something that works anyway. A way to do that, assuming you have one username and only the username on each line, would be to read the file (either as a whole or line by line), store somewhere only the lines that don't match the username and then overwrite the original file. – Federico klez Culloca May 23 '22 at 08:36
  • @user17395771 You can already remove one of the two questions by [edit]ing your question. Otherwise it will most likely be closed as "Needs more focus". – Ivar May 23 '22 at 08:39
  • 1
    You are [re] creating and closing the `Scanner` on every loop iteration. Don't close a `Scanner` that wraps `System.in` because when you do, you are effectively closing the standard input and you should not do that. Create the `Scanner` (that wraps `System.in`) once only, in your entire program and don't close it. – Abra May 23 '22 at 08:40
  • Took out the second question sorry guys, thanks for the advice so far I have taken the scanner out of the loop however the problem persists – user17395771 May 23 '22 at 09:10
  • @user17395771 please update your question with how you have taken the scanner out of the loop (without deleting the original part, just add it below the original code) so we can see whether you did it correctly and can fix it in case you didn't. – Federico klez Culloca May 23 '22 at 09:30
  • @FedericoklezCulloca just edited it into the post now – user17395771 May 23 '22 at 09:42

0 Answers0