0

The question is named Sparse Arrays in Problem Solving category on HackerRank and I am getting runtime errors. I have just started with Java and after Googling a lot I cannot debug it.

Error here on Imgur

public class Solution {
    private final static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        List<Integer> integers = new ArrayList<Integer>();
        List<String> strings = new ArrayList<String>();
        List<String> queries = new ArrayList<String>();
        int n = sc.nextInt();
        for( int i = 0; i < n; i++) {
            strings.add(sc.nextLine());
        }
        int q = sc.nextInt();
        for( int j = 0; j < q; j++) {
            queries.add(sc.nextLine());
        }
        
        for( int k = 0; k < q; k++) {
            int count = 0;
            for( int m = 0; m < q; m++) {
                if(queries.get(k) == strings.get(m)) {
                    count++;
                }
            }
            integers.add(count);
        }
        integers.forEach(System.out::println);
    }
}
Federico klez Culloca
  • 24,336
  • 15
  • 57
  • 93
  • The specific error is telling you you're trying to read the wrong type of data. It's probably caused by [this](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Federico klez Culloca Jun 05 '21 at 19:31

0 Answers0