Problem with my codes!

Hi everyone,

This is my code. It is an easy one but I am still getting a java.util.NoSuchElementException.
Not only for this code, but for various others. It is running successfully on Eclipse IDE.

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

/* Name of the class has to be “Main” only if the class is public. */
class Codechef
{
static String printAns(int x,int y,int k,int n,int arr[])
{
int left = x-y;
for(int i = 1;i<arr.length;i+=2)
{
if(arr[i] <= k)
{
if(arr[i-1] >= left)
return “LuckyChef”;
}
}
return “UnluckyChef”;
}

public static void main (String[] args)
{
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while (t>0)
	{
	    int x = sc.nextInt();
	    int y = sc.nextInt();
	    int k = sc.nextInt();
	    int n = sc.nextInt();
	    int arr[] = new int[n*2];
	    for(int i = 0;i<arr.length;i++)
	    {
	        arr[i] = sc.nextInt();
	    }
	    System.out.println(printAns(x,y,k,n,arr));
	    t--;
	}
}

}

1 Like

I guess the bug lies either in the input part, where the unit providing the input is not giving enough numbers for scanner to be happy, or maybe it is you who made the booboo of doubling the value of n and providing that as the size of the array. So, either:

int arr[] = new int[n].

or
the input should be something like this:

1
1 2 3 4
1 2 3 4 5 6 7 8

My bets are on the first part. Hope this helps! :slightly_smiling_face: