why my program on compiling showing such kind of exception
Exception like NoSuchElementException
hey @karthik1989photos
Are you in an online java batch.
Why are you coding in c language.
printf(“enter the value”); // remove this line in your code
and also remove this printf(“the subsequent sum becomes negative”);
print n if sum>=0
You try to code in java
No i m writing in java,but on compiling the code showing NoSuchElementException
send me your code(Java)
import java.util.*;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int sum=0;
while(sum>=0){
int n=sc.nextInt();
if(n>=-1000 && n<=1000){
sum = sum+n;
if(sum<0)
{
break;
}
System.out.println(n);
}
}
}
}
import java.util.*;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int sum=0;
while(sum>=0){
int n=sc.nextInt();
//if(n>=-1000 && n<=1000){ // redundant condition not Needed
sum = sum+n;
if(sum<0)
{
break;
}
System.out.println(n);
}
}
}
again after commenting that condition also showing same 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:9)
again showing same exception
Again send me your code
import java.util.*;
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int sum=0;
while(sum>=0){
int n=sc.nextInt();
//if(n>=-1000 && n<=1000){
sum = sum+n;
if(sum<0)
{
break;
}
System.out.println(n);
}
}
}
Have you changed java in Coding Blocks ide
just submit your code
kk done its working.
Please mark your doubts as resolved in your course’s.
