-2
Scanner  sc=new Scanner(System.in);
    int k=sc.nextInt();
    sc.nextLine();
    while(k-->0)
    {
        boolean t =false;
        int n=sc.nextInt();
        int l=sc.nextInt();

        int i,j,m;
        int a[]= new int[n];
        for(i=0;i<n;i++)
        {
            a[i]=sc.nextInt();

        }
        for(i=0;i<n-2;i++)
        {
            for(j=i+1;j<n-1;j++)
            {
                for(k=j+1;k<n;k++)
                {
                    if((a[i]+a[j]+a[k])==l)
                    {
                         t=true;
                        break;
                    }
                }
            }
        }
     String f=  t?"true":"false";
           System.out.println(f);
    }
    sc.close();

Exception in thread "main"

java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Solution.main(Solution.java:17)

Sample Input

3
5 60
1 20 40 100 80 

Sample output false

What did i tried?

if(sc.hasNextInt())
n=sc.nextInt();
if(sc.hasNextInt())
l=sc.nextInt();

I'm getting more duplicate outputs(i.e false) for the supposed hasNextInt() fix.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
Vermax
  • 3
  • 1
  • 1
  • 8
  • Did you step through your code in your IDE debugger to see what it is actually doing? Please do that, it will be MUCH more educational for you. If you do that and still don't understand, then explain EXACTLY what you found while debugging and what you don't understand. – Jim Garrison Sep 03 '17 at 04:28

3 Answers3

0
3
5 60
1 20 40 100 80 
false

This is what I am getting after running your code. Your code is correct I guess. But you are not giving input to it.

java.util.NoSuchElementException is thrown when scanner can not find what you asked it to read.

Give exactly correct input.

ram914
  • 331
  • 4
  • 19
0

It seems that you have closed the scanner before in your code. Do not close it & re-instantiate it in program, rather initialize it once at start of main program & close it at the end of main program.

Scanner is closed as its resources closed because it implements Closeable interface.

Like in below example -

  public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        System.out.println(System.in.available());
        scanner.close();
        scanner = new Scanner(System.in);
        // System.out.println(System.in.available());
        // If you uncomment above: It will give you java.io.IOException: Stream closed
        String str = scanner.nextLine();
  }

Derived this answer from post: java.util.NoSuchElementException - Scanner reading user input

Gurpreet Singh
  • 380
  • 4
  • 9
-1

put an iteration at the start, and check for condition like this:

do {
//Here the code
} while (i.hasNextInt());

and for the scanner error you should check if scanner has one like this

Scanner sc =new Scanner(System.in);
if (sc.hasNext()) {
//here the code
}