Why codechef not accepting my code, giving me java.util.NoSuchElementException

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 sc= new Scanner(System.in);
		int n = sc.nextInt();
		int a[]= new int[n];
		int c[] = new int[n];
		
		for(int i=0; i<a.length; i++)
		{
		    int b=sc.nextInt();
		    a[i]=b;// your code goes here
		}
		
		for(int i=0; i<a.length; i++){
		    int b=0;
		    for(int j=a[i]; j>0; j/=10){
		        b=b*10+(j%10);
		    }
		    System.out.println(b);
		
	
	    }
	}
	
	
	
}
1 Like

if thats the program for reversing a number then that works fine on giving some input.

Please share the question link.

I just did that on my local IDE.

try to run in your local compiler or online compiler

Hey…What I understand from your above code is “You are trying to reverse each and every element of the given array”. It’s working fine for me atleast.

Try to run again.

Encapsulate the entire code which is inside main inside a try catch block

Try giving custom input and then run

Use the hasNext() function of Scanner class before taking inputs to check is if there is any input on the input stream.

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 sc= new Scanner(System.in);
	    int n=0,b=0;
	    if(sc.hasNext())
		    n = sc.nextInt();
		int a[]= new int[n];
		int c[] = new int[n];
		
		for(int i=0; i<a.length; i++)
		{
		    if(sc.hasNext())
		        b=sc.nextInt();
		    a[i]=b;// your code goes here
		}
		
		for(int i=0; i<a.length; i++){
		    b=0;
		    for(int j=a[i]; j>0; j/=10){
		        b=b*10+(j%10);
		    }
		    System.out.println(b);
		
	
	    }
	}
}