I m new to coding , i got this error java.util.NoSuchElementException while i m executing help me why this error shows

import java.util.;
import java.lang.
;
import java.io.*;

public class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t–>0){
int n = sc.nextInt();
int c = sc.nextInt();
int[] a = new int[n];
int flag = 0;
for(int i =0 ; i<a.length ; i++){
a[i] = sc.nextInt();
c = c-a[i];
if(c<=0){
flag=1;
}
}
if(flag==0){
System.out.println(“Yes”);
}
else{
System.out.println(“No”);
}
}
}
}

Hey bro,you have done many mistakes,there many errors in your program, such as:
1>there is no such thing as import java.util in java,either import the whole package’s classes by import java.util.*; or import the specific class ie import java.util.classname;
same for other importing statements also.

2>in while loop it should be a decreament operator,ie you should have written,
while((t–)>0) ,like this,not a only neg(-) sign

3>if you are printing the string ,then we do not put this (“Yes”),but we put this (“Yes”) ,diff… of braces(or what you want it to call)

4>always try to enclose the code with try and catch block to prevent the NZEC exception.
Correct prog acc to you would be :-
import java.util.;
import java.lang.
;
import java.io.*;

public class Main
{
public static void main (String[] args)
{
Scanner sc = new Scanner(System.in);
try
{
int t = sc.nextInt();
while((t–)>0){
int n = sc.nextInt();
int c = sc.nextInt();
int[] a = new int[n];
int flag = 0;
for(int i =0 ; i<a.length ; i++){
a[i] = sc.nextInt();
c = c-a[i];
if(c<=0){
flag=1;
}
}
if(flag==0){
System.out.println(“Yes”);
}
else{
System.out.println(“No”);
}
}
}
catch(Exception e)
{
return e;
}
}
}

But I would recommend you that pls work on your basics first, do not move to questions directly, first be well versed with the basic syntax.
correct 1 mistake acc to step 1.

Your code contains every mistake you pointed out in his :slight_smile:

All those errors are caused by posting raw Java code without wrapping it in formatting tags - @reeshi please format your code - the forum software has mangled it and it won’t compile! :slight_smile:

@reeshi: I see no submissions from you, so I’m guessing you are clicking Run without providing Custom Input.

1 Like

The NoSuchElementException in Java is thrown when one tries to access an iterable beyond its maximum limit. This means that, 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.

As with most programming languages, the Iterator class includes a hasNext() method that returns a boolean indicating if the iteration has anymore elements. If hasNext() returns true, then the next() method will return the next element in the iteration otherwise raise exceptions if the iteration has no more elements.

if(input.hasNextInt() )
     number1 = input.nextInt(); // if there is another number  
else 
     number1 = 0; // nothing added in the input