Nalin Nishant
Nalin Nishant

Reputation: 896

java.util.NoSuchElementException : No line found

I already searched every question related to this error on StackOverflow but trust me my case is different here. actually, I am preparing for competitive level programming skills and I solved this question successfully on the laptop and the output is 100%true without any type of error for this question of hacker earth (link below)-

https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/#c198374

but when I try to submit my code on hacker earth it throws an error and now I am really confused that why error comes when it successfully ran on my laptop. the error screenshot below - enter image description here here is my code -

import java.util.*;

class nalin{
    public static void main(String args[]){
        Scanner bob = new Scanner(System.in);
        int bobs = bob.nextInt();
        String result[] = new String[bobs];
        for(int g = 0; g<bobs; g++){
        Scanner s = new Scanner(System.in);
        String x = s.nextLine();
        String arr[] = x.split("\\s+");
        int coun = 0;
        char v1[] = arr[0].toCharArray();
        char v2[] = arr[1].toCharArray();

        for(int i = 0; i<v1.length; i++){
            for(int j = 0; j<v2.length; j++){
                if(v1[i] == v2[j]){
                    coun = coun+1;
                    break;
                }
            }
        }
        if(coun == v1.length){
            result[g] = "YES";
        }else{
            result[g] = "NO";
        }
     }
    for(int l = 0; l<result.length; l++){
        System.out.println(result[l]);
    }
    }
}

Upvotes: 1

Views: 953

Answers (2)

SSP
SSP

Reputation: 2670

Note : Use Hashing Concept Only . Try to do it in O(string length). Mention in question itself. and you logic is also wrong( check nested loop which you used for string comparision.) Also you have decelared scanner 2 times. remove 1 inside for loop

You are using nextLine method of scanner class. Below is nextline() description

nextLine
public String nextLine()


    Advances this scanner past the current line and returns the inputthat was skipped.This method returns the rest of the current line, excluding any lineseparator at the end. The position is set to the beginning of the nextline. 
    Since this method continues to search through the input lookingfor a line separator, it may buffer all of the input searching forthe line to skip if no line separators are present.

    Returns:the line that was skipped

Throws:NoSuchElementException 
 1 - if no line was foundIllegalStateException 
 2 - if this scanner is closed.

Below is working code - But I have not corrected your logic.

package Array;

import java.util.*;

class nalin {
    public static void main(String args[]) {
        Scanner bob = new Scanner(System.in);
        int bobs = bob.nextInt();
        String result[] = new String[bobs];
        for (int g = 0; g < bobs; g++) {
            String x = bob.next();
            String y = bob.next();
            //String arr[] = x.split("\\s+");
            int coun = 0;
            char v1[] = x.toCharArray();
            char v2[] = y.toCharArray();

            for (int i = 0; i < v1.length; i++) {
                for (int j = 0; j < v2.length; j++) {
                    if (v1[i] == v2[j]) {
                        coun = coun + 1;
                        break;
                    }
                }
            }
            if (coun == v1.length) {
                result[g] = "YES";
            } else {
                result[g] = "NO";
            }
        }
        for (int l = 0; l < result.length; l++) {
            System.out.println(result[l]);
        }
    }
}

Upvotes: 1

mangusta
mangusta

Reputation: 3544

Put the scanner declaration outside of the loop:

    Scanner s = new Scanner(System.in);
    int bobs = s.nextInt();
    for(int g = 0; g<bobs; g++)
    {
    }

Use a single scanner throughout the execution

Upvotes: 0

Related Questions