Please help #NoSuchElementException

import java.io.*;
import java.util.*;
import java.lang.*;
class Codechef {
	public static void main(String[] args) throws java.lang.Exception {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-- > 0) {
			int dishes = sc.nextInt();
			int arr[] = new int[dishes];
			for(int i = 0; i < dishes; i++) {
				if(sc.hasNextInt()) {
					arr[i] = sc.nextInt();
				}
		    }
			Arrays.sort(arr);
			int a = 0;
			int b = 0;
			for(int i = dishes - 1; i >= 0; i--) {
				if(a >= b) b += arr[i];
				else a += arr[i];
			}
			System.out.println(a > b ? a : b);
		}
	}
}

I’m getting this error while solving the problem. Where to edit the code to handle this error? Please help.


.

Wrong name for main class, you cannot just name it class Main, name it class Codechef.

1 Like

That’s not the issue tho. But I did it as u said. Showing the same error.

This problem only arises when you try only running the code without any custom input, whereas if you would try running your cone on some custom input or directly submitting the code the error might not show.

1 Like

And you are taking input in the array the wrong way. You are overriding the first array element everytime you take an array element input because of the while loop. The while loop is totally unnecessary here.

1 Like

Can you please edit the code and submit? You can copy the code from above.

The above code is giving AC, you can just submit it!

1 Like

Thanks a lot. :black_heart: