1

well im currently learning java my myself but from what i know i just cant seem to fix this problem currently testing a script where if u dont type ur name exactly u must re-type it but this error appears i searched everywhere but most of the things i tried dont work

Please type in your name: 
lucas
Welcome lucas
Confirm your name:
luca
Please type in your name: 
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at input.main(input.java:9)

here is the code:

import java.util.Scanner;

public class input {
    public static void main(String[] args) {

        while (true) {
            try (Scanner input = new Scanner(System.in)) {
                System.out.println("Please type in your name: ");
                String name = input.nextLine();
                System.out.println("Welcome " + name);
                if (name.equals("nico")) {
                    System.out.println("bitch");
                    break;
                } else {
                    System.out.println("Confirm your name:");
                    String name1 = input.nextLine();
                    if (name1.equals("nico")) {
                        System.out.println("Hello " + name1 + "... bitch");
                    } else if (name1.equals(name)) {
                        System.out.println("Thank you");
                        break;
                    }
                }
            }
        }
    }
}
2
  • Welcome to SO. Please read How to Ask Commented Jan 13, 2015 at 22:35
  • What input did you use to get the output above? The NoSuchElementException is being raised because Scanner.nextLine() is not finding another line. Commented Jan 13, 2015 at 22:41

2 Answers 2

2

Move the try-with-resources to around your while loop. When execution leaves the try-with-resources, Java closes the resources. Here, that resource is the standard input, which cannot be re-opened.

try (Scanner input = new Scanner(System.in)) {
    while (true) {
        System.out.println("Please type in your name: ");

You actually don't really need the try-with-resources here. Don't close standard input/output/error.

Sign up to request clarification or add additional context in comments.

3 Comments

And now I learned something today! Documentationf for the interested: docs.oracle.com/javase/7/docs/api/java/util/…
wow i cant belive i didnt notice that... i feel dumb now but lets say i remove the try-with-resources and its left like this: Scanner input = new Scanner(System.in); while (true) { System.out.println("Please type in your name: "); the IDE tells me Resource leaked: the 'input' is never closed. unless i keep the try-with-resources
@lucas That's a warning. You can choose to ignore it. In this case, you should ignore it.
1

Don't put the Scanner into your loop.

Loop while the scanner still has input.

Currently, you create new Scanner too often.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.