0

I have two methods that ask for input from the keyboard. In one method it works fine. In the other I get the error:

Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Unknown Source)

Here are the methods:

This one works fine

public static int logon(Connection conn,boolean flag) throws Exception{       

    Scanner kboard   = new Scanner(System.in);
    boolean exit     = false;        
    int     usr      = -1,
            spaces   = 0,
            commaChk = 0;                
    String input;        
    String[] tokens  = new String[3];

    //ask for user input
    do{
        printHeader(0);

        System.out.print( "Please enter first name, last name and password to logon or create a new account \n" +
                          "use a space to seperate entries, no commas                                     : ");

        input = kboard.nextLine();

        if(input.length() == 0) exit = true;

This one returns the error

public static int generalPrompt()throws Exception{
    Scanner kboard = new Scanner(System.in);        
    String input; 
    int selection = 0;
    String[] myInts ={"1","2","3","4","5","6"};
    boolean goodInput = false;

    do{               
        printHeader(0);
        printHeader(1);

        System.out.print("\n\n Select number from above ([return] to exit) : ");

        input = kboard.nextLine();

        if (input.length() == 0){
Thelouras
  • 852
  • 1
  • 10
  • 30
DCR
  • 14,737
  • 12
  • 52
  • 115
  • 1
    Post a MCVE. Do you call `kboard.close()` in `logon`? – Elliott Frisch Dec 22 '18 at 21:31
  • Exactly as @ElliottFrisch -- I fear that you might be closing the `System.in` stream by creating and closing multiple Scanner objects. – Hovercraft Full Of Eels Dec 22 '18 at 21:32
  • I do close kboard in the logon method but shouldn't the scanners be local to the method? – DCR Dec 22 '18 at 21:33
  • You should only have **one** Scanner object that uses `System.in` in your program, and pass it where needed. Else you risk closing that critical stream. – Hovercraft Full Of Eels Dec 22 '18 at 21:34
  • @DCR *I do close kboard in the logon method but shouldn't the scanners be local to the method?* `Scanner`(s) yes. `System.in` is a **global**. And when you close a `Scanner` on `System.in` it also closes `System.in` (which you can't reopen). – Elliott Frisch Dec 22 '18 at 21:34
  • Interesting, thank you, Odd that it works in Bluej but not eclipse – DCR Dec 22 '18 at 21:37

0 Answers0