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)
CODE :
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
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);
}
}
}
Your code is giving NZEC error since it is stuck in an infinite loop. You forgot to decrement t(test cases) count.
Just add one line inside while loop t = t-1; and you will get AC.
Here is the link to your correct code.
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)
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 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
}