Error using Scanner: Exception in thread "main" java.util.NoSuchElementException

Asked 2 years ago, Updated 2 years ago, 46 views

When using scanner in Java, the following error occurs when running atCoder:

error messages:

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:1594)
    at java.base/java.util.Scanner.nextInt (Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt (Scanner.java:2212)
    at Main.main (Main.java:10)

Current State Code:

import java.util.Scanner;
public class ABC086C {

    public static void main(String[]args) {
        Scanner sc = new Scanner (System.in);
        
        int[] location = {0,0};
        int n = sc.nextInt();
        int x1 = 0;
        int x2 = 0;
        for(inti=0;i<n;i++){
            int = sc.nextInt();
            x1 = sc.nextInt();
            x2 = sc.nextInt();
            location[0] = 0;
            location[1] = 0;
            for(int j=1;j<=t;j++){
                if(location[0]<x1&location[1]<x2){
                    location[0]+=1;
                } else if(location[0]<x1&location[1]==x2){
                    location[0]+=1;
                } else if(location[0]<x1&location[1]>x2){
                    location[0]+=1;
                } else if(location[0]>x1&location[1]<x2){
                location[1]+=1;
                } else if(location[0]>x1&location[1]==x2){
                    location[0]-=1;
                } else if(location[0]>x1&location[1]>x2){
                    location[0]-=1;
                } else if(location[0]==x1&location[1]<x2){
                    location[1]+=1;
                } else if(location[0]==x1&location[1]==x2){
                    location[0]-=1;
                } else if(location[0]==x1&location[1]>x2){
                    location[1]-=1;
                }
            }
            if(location[0]==x1&location[1]==x2){
                System.out.println("Yes");
            } else {
                System.out.println("No");
        }
        }
    }
}

java

2022-09-30 18:11

1 Answers

I don't know exactly what happened when "running on atCoder" was used, but NoSuchElementException appears to be caused by nextInt().

For example, if you execute the code in the question statement at the terminal and send an EOF with Ctrl+D, the exception will be sent.


2022-09-30 18:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.