This code was running well in eclipse but when I tried to compile it online it showed this 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 Main.main(Main.java:15)
public static void main (String[] args)throws IOException
{
Scanner input = new Scanner(System.in);
int t=input.nextInt();//the error refers to this line
int ctr;
double i;
while(t!=0)
{
ctr=0;
double n=input.nextDouble();
for(i=1;i<=12;i++)
{
ctr=(int) (ctr+Math.floor(n/5));
n=n/5;
}
System.out.println(ctr);
}
}
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:53)
/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
public static void check(String a,String b,int c)
{
int p=c;
int unique=0,unique2=0;
String str1=a;
String str2=b;
for(int i=0;i<p;i++)
{
for(int j=0;j<p;j++)
{
if(str1.charAt(i)==(str1.charAt(j)))
{
break;
}//if
unique++;
}
}
for(int i=0;i<p;i++)
{
for(int j=0;j<p;j++)
{
if(str2.charAt(i)==(str2.charAt(j)))
{
break;
}//if
unique2++;
}//for j
}// for i
max=unique;
if(unique2>max)
{
max=unique2;
}
}
//static int t=0;
static int max=0;
public static void main (String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int n=0;
for(int k=0;k<t;k++)
{
if(sc.hasNextInt()){
n=sc.nextInt();
}
String s1=sc.nextLine();
String s2=sc.nextLine();
//int unique=0,unique2=0;
check(s1,s2,n);
for(int i=0;i<n;i++)
{
if(s1.charAt(i)!=s2.charAt(i))
{
s1=s1.replace(s1.charAt(i),s2.charAt(i));
s2=s2.replace(s2.charAt(i),s1.charAt(i));
}//if
check(s1,s2,n);
}//a
System.out.println(max);
}//k
}//main
When using Scanner class for input, check if the Scanner object sc hasNextInt() and/or hasNext() or not for int and string inputs lets say, before just trying to take the input.
And this will resolve the error.
Example -
For string input :
I have same issue.My code is not going into infinite loops.
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int l=0;l<t;l++){
int n=sc.nextInt();
int arr[]= new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int sum=arr[n-1];
int m=1000000007;
for(int j=1,i=n-2;i>=0;i–,j++){
if(arr[i]>1)
sum+=(arr[i]-j);
sum%=m;
}
System.out.println(sum);
}
}
Wow! It surprisingly worked on codechef! Like I wasted an entire day to figure out whats wrong in my code. It worked and its great but can you please give a satisfying explanation of why it worked and whats wrong if i do not add it to my code? Please!!!
I am also getting the same error any suggestions, please-
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.nextDouble(Scanner.java:2413)
at Main.main(Main.java:14)
/* 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);
System.out.println("");
double x = sc.nextDouble();
/* 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
{
// your code goes here
Scanner sc = new Scanner(System.in);