0

I would like to fix the java.util.NoSuchElementException bug. I keep getting the error:

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

with this code

import java.util.Scanner;
import java.io.*;
class Main2
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        input.close();
        newUser();
    }
    private static void newUser()
    {
        try
        {
            Scanner input = new Scanner(System.in);
            System.out.println("Please enter the name for the new user.");
            String userNameNew = input.nextLine();
            System.out.println("Please enter the password for the new user.");
            String userPassWordNew = input.nextLine();
            System.out.println("The new user: " + userNameNew + " has the password: " + userPassWordNew + "." );
            PrintWriter out = new PrintWriter("users.txt");
            out.print(userNameNew + "\r\n" + userPassWordNew);
            out.close();
            input.close();
        } catch (IOException e) { e.printStackTrace(); }
    }
}

Can you please help me? Thank you.

  • 7
    Remove the first 2 lines of your main() method. – George Z. Apr 14 '19 at 23:18
  • thanks, George that worked. if you post it as an answer and not a comment I can mark it as a fix. – Felipe the Sheepy Apr 14 '19 at 23:24
  • BTW, just removing the lines is good, but doesn't explain the Exception. The reason is that by calling `close()` on your Scanner, it will also close its underlying stream (in this case, `System.in`), and once closed, you can't use it again. In your example code, after calling `newUser()`, your `System.in` will end up in a closed state, and you'll never be able to use it again in the rest of your code. – NPras Apr 14 '19 at 23:42
  • 1
    Possible duplicate of [java.util.NoSuchElementException - Scanner reading user input](https://stackoverflow.com/questions/13042008/java-util-nosuchelementexception-scanner-reading-user-input) – Benjamin Urquhart Apr 15 '19 at 00:11

1 Answers1

1

I found the reason why you are getting this exception.

So in your main method, you initialized your Scanner class object and immediately close it.

Here is the problem. Because when scanner calls the close() method, it will close its input source if the source implements the Closeable interface.

When a Scanner is closed, it will close its input source if the source implements the Closeable interface.

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

And InputStream class which is the input source in your case implements the Closeable interface.

https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html

And further you initialized the Scanner class object into your newUser() method. Here scanner class object initialized successfully but your input source is still close.

So my suggestion is that close scanner class object only once. Please find the updated code of yours.

    class Main2
    {
        public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        newUser(input); 
      //input.close()
    }
    private static void newUser(Scanner input) 
    {
        try {
            System.out.print("Please enter the name for the new user.");
            String userNameNew = input.nextLine();
            System.out.println("Please enter the password for the new user.");
            String userPassWordNew = input.nextLine();
            System.out.println("The new user: " + userNameNew + " has the password: " + userPassWordNew + "." );
            PrintWriter out = new PrintWriter("users.txt");
            out.print(userNameNew + "\r\n" + userPassWordNew);
            out.close();
        } catch (IOException e) { e.printStackTrace(); }
    }
}
Jeet Kumar
  • 555
  • 3
  • 12