Reputation: 594
Why I'm getting error in following Code? In the following code, getMaximumWinning is a function, which returns a positive integer.
*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)*
Code:
public static void main(String args[])
{
int TC;
Scanner s1=new Scanner(System.in);
TC= s1.nextInt();
int N,i;
int win[]=new int[10000];
String ques,ans;
while(TC>0)
{ N=s1.nextInt();
ques=s1.next();
ans=s1.next();
for(i=0;i<=N;i++)
{
win[i]=s1.nextInt();
}
System.out.println(getMaximumWinning(ques, ans, win, N));
TC--;
}
}
Upvotes: 0
Views: 2185
Reputation: 1579
java.util.NoSuchElementException
indicates that you have no more elements presents in Scanner
before any s1.nextInt();
you should use s1.hasNextInt();
which returns boolean value depending on which you can decide what action to perform
if(s1.hasNextInt()){
win[i]=s1.nextInt();
}
Upvotes: 1