Naveen Kumar
Naveen Kumar

Reputation: 424

arrayException in thread "main" java.util.NoSuchElementException

You are playing a game on your cellphone. You are given an array of length n, indexed from 0 to n−1. Each element of the array is either 0 or 1. You can only move to an index which contains 0. At first you are at the 0th position. In each move you can do one of the following things:

Walk one step forward or backward. Make a jump of exactly length mm forward. That means you can move from position x to x+1, x−1 or x+m in one move. The new position must contain 0. Also you can move to any position greater than n-1.

You can't move backward from position 0. If you move to any position greater than n−1, you win the game.

Given the array and the length of the jump, you need to determine if it's possible to win the game or not.

sample STDIN:

1
86 95
0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 1 0 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 1 0 1 1 0 1 1 0 0 1 0 0 1 0 1 1 0 0 1 1 1 0 1 1 0 0 0

/here 1 in no. of test cases, n is 86, m is 95, then follows 86 elements of array/

Errors incurred:

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:45)

I am not able to figure out why i am getting this compilation error although some of the test cases are working fine?

here is my code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static Boolean track(int[] arr,int pos,int n, int[] add, int[] marked_arr)
    {
        if(pos>=n)
            return true;
        for(int i=0;i<3;i++)
        {
            int new_pos=pos+add[i];
            if(new_pos>=0) //valid jumping position in array
            {
                if(new_pos<n) 
                {
                    if(arr[new_pos]!=0)
                        continue;
                    if(marked_arr[new_pos]==-2)
                        return false;
                    marked_arr[new_pos]=-2;
                }
                if(track(arr,new_pos,n,add,marked_arr))
                    return true;
                else
                    marked_arr[new_pos]=-1;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scanner = new Scanner(System.in);
        int t = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < t; i++) {
            int n = scanner.nextInt();
            int m = scanner.nextInt();

            int[] arr = new int[n];
            for (int j = 0; j < n; j++) 
                arr[j] = scanner.nextInt();

            int[] marked_arr= new int[n];
            Arrays.fill(marked_arr, -1); //if some position is visited once , we will make that position -2

            int[] add={m,1,-1};
            Boolean ans=track(arr,0,n,add,marked_arr);
            if(ans)
                System.out.println("YES");
            else
                System.out.println("NO");

        }
    }
}

Upvotes: 1

Views: 95

Answers (2)

Andy Turner
Andy Turner

Reputation: 140319

then follows 86 elements of array

There are only 81 elements in the line:

0 0 1 0 0 0 1 ...

but you are trying to read 86.

Upvotes: 0

Darkwing
Darkwing

Reputation: 314

Problem is with this line:

 arr[j] = scanner.nextInt();

You are trying to get next element, which doesn't exist. Please do check before calling .nextInt:

if (scanner.hasNextInt()) {
    arr[j] = scanner.nextInt();
}

Also you can modify your loop with break or to use while (scanner.hasNext()) {} loop

Upvotes: 1

Related Questions