Home>

I only did C language in a university lecture, and I have very little programming experience.
Currently, I am studying Java by myself based on a certain book, but when I run the following source code using paiza.IO, I get a Runtime error.
I didn't know if I could put the problem statement, so I will put only the source code.

Could you please tell me why such an error occurs and how I can resolve it?
Please.

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.base/java.util.Scanner.nextLine (Scanner.java:1651)
    at Main.main (Main.java:6)
Corresponding source code
import java.util. *;
public class Main {
    public static void main (String [] args) {
        int isHungry = new java.util.Scanner (System.in) .nextInt ();
        String food = new java.util.Scanner (System.in) .nextLine ();
        System.out.println ( "Hello");
        if (isHungry == 0)
        {
            System.out.println ("I'm full.");
        }
        else else
        {
            System.out.println ("Harapeko");
            System.out.println (food + "I'll have");
            System.out.println ("Feast");
        }
    }
}
What I tried

6th line
String food = new java.util.Scanner (System.in) .nextLine ();
I have a clue that there may be a problem with this part, but I didn't know why it was wrong, and I searched the net for this error, but I couldn't find it at all.

Supplementary information (FW/tool version, etc.)

Usage environment: paiza.IO (online execution environment)

  • Answer # 1

    import java.util. *;
    // Main class
    public class Main {
        public static void main (String [] args) {
            // Exception handling
            try {
                // Create an instance of the Scanner class
                Scanner scanner = new Scanner (System.in);
                // Use the nextLine method of the Scanner class
                String food = scanner.nextLine ();
                // Use the nextInt method of the Scanner class
                int isHungry = scanner.nextInt ();
                // if isHungry is 0
                if (isHungry == 0) {
                    System.out.println ("I'm full.");
                }
                // If not 0
                else {
                    System.out.println ("Harapeko");
                    System.out.println (food + "I'll have");
                    System.out.println ("Feast");
                }
            // If an exception occurs when the input is incorrect
            } catch (NoSuchElementException e) {
                System.out.println ("Enter the correct input");
            }
        }
    }

    I think you can go with the above contents.
    According to the specifications of paiza.io, you can enter yakiniku + line feed + number (* trial "1") in "input".
    Teka, paiza.io is really convenient! !!
    * I myself java.util.Scanner I'm not good at writing like this, right?
    It is better to use the function nextLine () in the class that is instantiated and made available by having an entity.
    Is that okay?
    For the time being, I think this will work, but I think it would be nice if you could find out why this works (laughs). .. .. Please tell me secretly if you can (laugh)

  • Answer # 2

  • Answer # 3


    scanner.close ();and import java.util.Scanner;I don't need it (laughs)
    This kind of thing seems to be addictive. .. ..
    Try&error! !!