NoSuchElementException for nextInt

compiling below code gives an error, can someone help me in resolving this?

import java.util.;
import java.lang.
;
import java.io.*;
import java.util.Scanner;

/* Name of the class has to be “Main” only if the class is public. */

class Codechef

{

public static void main (String[] args) 

{ 

    int a,b; 

    Scanner input = new Scanner(System.in);

    a=input.nextInt(); 

    b=input.nextInt(); 

        int diff = 0, output1 = 0, output2 = 0, diffDigits =0; 

    diff = a-b; 

} 

}

just give proper inputs and run it… U wont get any error :slight_smile:
JAVA yells when it doesn’t get required inputs…

10 Likes

import java.util.*;

import java.lang.*;

import java.io.*;

class Codechef

{

public static void main (String[] args)

{

int a,b;

Scanner input = new Scanner(System.in);

a=input.nextInt();

b=input.nextInt();

System.out.println(a-b);

}

}

above code is still giving below error and NZEC as Runtime error

Exception in thread “main” java.util.NoSuchElementException

at java.util.Scanner.throwFor(Scanner.java:862) 

at java.util.Scanner.next(Scanner.java:1485) 

at java.util.Scanner.nextInt(Scanner.java:2117) 

at java.util.Scanner.nextInt(Scanner.java:2076) 

at Codechef.main(Main.java:11) 
2 Likes

Try using BufferedReader. Worked out fine for me. A scenario you have to keep in mind is if the user enters NULL value i.e. simply presses enter. In that case you should surround your input in a Try block, while Catch just returns.

Ex :

try
{cases = Integer.parseInt(br.readLine());}
catch(Exception e)
{return;}

3 Likes

Read my answer… Give inputs to compiler and error will vanish… and you are getting wrong answer because your logic is incorrect…

2 Likes

I am always getting this Error . here is my Code:

import java.util.*;
public class CodeChef{

    public static void main(String args[] ) throws Exception {
    	
    	Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for(int i=0;i < t; i++ )
		{
		    int N = sc.nextInt();
		    int a = sc.nextInt();
		    int b = sc.nextInt();
		    int c = sc.nextInt();
		    
		    int F[] = new int[N];
		    
		    for(int j =0 ;j<N; j++ )
		    {
		        F[j]=sc.nextInt();
		    }
		  Arrays.sort(F);
		  long res = Math.abs(b-F[0])+ Math.abs(F[0]-a)+c ;
		    System.out.println(res);
		  
		}
		
    }

}

2 Likes

Click custom input and give your inputs … then click run… hope it helps

6 Likes

For Custom inputs, it is working fine, but when i am running it is again giving me same error. This is happening for every problem’s solution I am trying to submit.
Again for this:
/* package codechef; // don’t place package name! */

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args)
{
// your code goes here
Scanner sc = new Scanner(System.in);
// int res =0;
int r = sc.nextInt();
int rem = r%4;
if(rem==0){
System.out.println(r);
}
else if(rem==1)
System.out.println(1);
else if(rem==2)
System.out.println(r+1);
else
System.out.println(0);
}
}

1 Like

@monika_1011
You code is perfectly fine. But your issue is specific.

Read Constraints.

1 Like

I am always getting this Error. Even I Tried to submit the other user solution that are accepted for them, i am getting the same Runtime Error.NZEC, No Such Element Found. :disappointed:

10 Likes

AFAIK, you were trying to solve this CodeChef: Practical coding for everyone ?

yes

See the constraints of R.

yes,Got it fixed. Thanks

this error/exception comes when there is no input to the scanner .
so use below piece of code to avoid it.
for(int i=1;i<=n;i++) { if(s.hasNextInt()) a[i]=s.nextInt(); }

3 Likes

The NoSuchElementException in Java is thrown when one tries to access an iterable beyond its maximum limit. This means that, this exception is thrown by various accessor methods to indicate that the element being requested does not exist . The next() method in Java returns the next element in the iteration or NoSuchElementException if the iteration has no more elements.

As with most programming languages, the Iterator class includes a hasNext() method that returns a boolean indicating if the iteration has anymore elements. If hasNext() returns true, then the next() method will return the next element in the iteration otherwise raise exceptions if the iteration has no more elements.

if(input.hasNextInt() )
     number1 = input.nextInt(); // if there is another number  
else 
     number1 = 0; // nothing added in the input
3 Likes

what changes did you do
because i am also getting same eror for every problem i submit

2 Likes

You need to provide the Custom Input before Running the code.

4 Likes

import java.util.;
import java.lang.
;
import java.io.*;

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner kb = new Scanner(System.in);
int withdrawlAmt = kb.nextInt();
double initialBalance = kb.nextDouble();
if(withdrawlAmt % 5 == 0) {
if(withdrawlAmt < initialBalance) {
System.out.printf(“%.2f”, (initialBalance - withdrawlAmt)-0.5);
}
else {
System.out.printf(“%.2f”, initialBalance);
}
}
else {
System.out.printf(“%.2f”, initialBalance);
}
}
}
here’s my code can any tell me why i am getting NoSuchElementFoundException

I am unable to resolve

import java.io.;
import java.util.
;
import java.lang.*;
class Main {

public static void main(String[] args)throws Exception
{
   
       
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(br.readLine());
    int i,c;
    for(i=0;i<n;i++)
    {
        int a=Integer.parseInt(br.readLine());
        int b=Integer.parseInt(br.readLine());
        c=a%b;
        System.out.println(c);
        
    }
   }
   catch(Exception e)
   {
       System.out.println(e);
   }
}

}