0

I've made method taking non blank input so I can validate input to my list.

public String getNonBlankInput(String text){

    Scanner scan = new Scanner(System.in);

    System.out.println(text);
    String input = scan.nextLine();
    while (input.isEmpty() || input.equals(" ")) {
        System.out.println("Input is empty.");
        System.out.println(text);
        input = scan.nextLine();
    }
    scan.close();

    return input;

}

I would like to use this in my method which adds objects to my LinkedList. Here is code of this method:

public void addMenu(){
    String log = getNonBlankInput("Enter login: ");
    String pass = getNonBlankInput("Enter password: ");
    String web = getNonBlankInput("Enter website: ");

    Entry newEntry = new Entry(web,log,pass);
    edao.addEntry(newEntry);
}

The problem is, whatever I've put as login, password or website I'm getting Exception:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at com.password.PasswordManager.data.InputValidation.getNonBlankInput(InputValidation.java:12)
at com.password.PasswordManager.input.MenuImplementation.addMenu(MenuImplementation.java:15)

Anyone have a clue what is wrong here? Before I've created method getNonBlankInput everything was ok.

exover
  • 5
  • 3
  • 2
    the problem may be that you are creating and closing multiple `Scanner` objects. Essentially, when you close the `System.in` stream, it cannot be opened again. [See here for more details](https://stackoverflow.com/questions/48116815/exception-in-thread-main-java-util-nosuchelementexception-no-line-found-3). A suggestion is to pass in a `Scanner` object for `getNonBlankInput()` and close the scanner after `edao.addEntry()`. This should fix your problem. – user3170251 Aug 01 '18 at 19:06
  • https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found – Crammeur Aug 01 '18 at 19:21
  • 2
    @snr `Scanner scan = new Scanner (System.in)` is being called 3 times from 3 `getNonBlankInput()` calls. – user3170251 Aug 01 '18 at 19:21
  • @user3170251 yes, exactly. – Soner from The Ottoman Empire Aug 01 '18 at 19:24
  • 3
    @snr, each call of `getNonBlankInput` will open and close a scanner on `System.in` closing that stream... once close, `System.in` can't be open again. – AxelH Aug 01 '18 at 19:26

3 Answers3

0

The problem may be that you are creating and closing multiple Scanner instances. Essentially, when you close the System.in, it cannot be opened again. A suggestion is to create a Scanner instance in addMenu()and pass in that scanner to getNonBlankInput() and close the scanner after edao.addEntry().

public void addMenu(){
    Scanner scan = new Scanner (System.in);

    String log = getNonBlankInput("Enter login: ", scan);
    String pass = getNonBlankInput("Enter password: ", scan);
    String web = getNonBlankInput("Enter website: ", scan);

    Entry newEntry = new Entry(web,log,pass);
    edao.addEntry(newEntry);
    scan.close();
}

public String getNonBlankInput(String text, Scanner scan){
    ...
}
user3170251
  • 310
  • 1
  • 13
0

You are using three instance of java.util.Scanner in addMenu method. You have to used only one instance of java.util.Scanner. Remove the instance of java.util.Scanner and Scanner's close(), then add this as class variable.

static Scanner input = new Scanner(System.in);

Make sure you close input when you done with it.

  • Thanks everyone for your help and time. I have used one Scanner and decided not to close it. – exover Aug 02 '18 at 20:11
0

I tried your code and is when create multiple instantiation of Scanner target System.in then close after first then this error append.

What I suggest you is to use only one Scanner or don't close it and this work too.

Example

public void addMenu() {
    Scanner scan = new Scanner(System.in);

    String log = getNonBlankInput("Enter login: ", scan);
    String pass = getNonBlankInput("Enter password: ", scan);
    String web = getNonBlankInput("Enter website: ", scan);

    scan.close();
    ...
}

public String getNonBlankInput(String text, Scanner scan){

    System.out.println(text);
    String input = scan.nextLine();
    do {
        if (input.isEmpty() || input.equals(" ")) {
            System.out.println("Input is empty.");
            System.out.println(text);
            input = scan.nextLine();
        }

    } while ((input.isEmpty() || input.equals(" ")) && scan.hasNextLine());

    return input;

}

Example 2

public void addMenu() {
    String log = getNonBlankInput("Enter login: ");
    String pass = getNonBlankInput("Enter password: ");
    String web = getNonBlankInput("Enter website: ");
    ...
}

public String getNonBlankInput(String text){
    Scanner scan = new Scanner(System.in);
    System.out.println(text);
    String input = scan.nextLine();
    while (input.isEmpty() || input.equals(" ")) {
        System.out.println("Input is empty.");
        System.out.println(text);
        input = scan.nextLine();
    }
    //scan.close();
    return input;
}
Crammeur
  • 678
  • 7
  • 17