简体   繁体   中英

How can I fix java.util.NoSuchElementException

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.

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(); }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM