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:5)
WHEN I RUN THE CODE IN ECLIPSE IT WORKS FINE AND IN CODING BLOCKS IDE IT GIVES THE ABOVE MENTIONED ERROR
ERROR java.util.NoSuchElementException
Have you selected the language as JAVA??
YES I HAVE SELECTED JAVA
have you given some custom input??
HERE IS THE CODE
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int i,j;
if(n<=1000){
for(i=1;i<=n;i++){
for(j=1;j<=i;j++)
{
if (i==1){
System.out.print(“1”);}
else {
if (j==1){
System.out.print(i-1);}
else if (j==i)
{
System.out.print(i-1);
}
else{System.out.print("0"); }}
}
System.out.println();
}
}else{
System.out.println("value of n is greater than 1000");
}
}
}
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. The solution to this exception is to check whether the next position of an iterable is filled or empty. You should only move to this position if the check returns that the position is not empty. The following methods are used to check the next position:
- hasNext()
- hasMoreElements()