Home>

I am learning about reading multiple data with standard input in paiza learning.

This is an issue that displays "Slime Appears" as many times as it appears on the first line.

Why do I get the following error?

Numbers are entered in the input area.

Error message
Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor (Scanner.java:937)
    at java.base/java.util.Scanner.next (Scanner.java:1478)
    at Main.main (Main.java:11)
(Exit status: 1)
### Corresponding source code


java code

// Standard input and loop processing
import java.util. *;
public class Main {
    public static void main (String [] args) {
        Scanner sc = new Scanner (System.in);
        int count = sc.nextInt ();
        String data;
        for (int i = 0;i<count;i ++) {
            data = sc.next ();
            System.out.println ("Slime appeared");
       }
    }
}
  • Answer # 1

    // Standard input and loop processing
    import java.util. *;

    public class Main {
    public static void main (String [] args) {
    Scanner sc = new Scanner (System.in);
    int count = sc.nextInt ();

    String data;
    for (int i = 0;i System.out.println ("Slime appeared");
    }
    }
    }