Search code examples
javaarraysexceptionstdoutinputmismatchexception

Why am I not getting any output? And there is an InputMismatchException


I am trying to solve a practice problem on hackerrank. But, my output is not displaying and the program throws an InputMismatchException can someone please help me out?

Input Format

  • The first line contains and integer n, the size of variable strings.
  • Each of the next n lines contains a string strings[a] .
  • The next line contains q, the size of queries.
  • Each of the next q lines contains a string queries[b].

Output Format

Return an integer array of the results of all queries in order.

example input:

4
aba
baba
aba
xzxb
3
aba
xzxb
ab

example output :

2
1
0
-------------------------------------------------------------------------  
/*my program*/

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.Arrays;

class Solution {
    public static void main(String[] args) {
    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    String strings[] = new String[n];
     int count = 0;
    int i,j=0;
    for(int a=0 ; a<n ; a++){
        strings[a] = s.nextLine();
    }

    int q = s.nextInt();
    String queries[] = new String[q];
    for(int b=0; b<q;b++){
        queries[b] = s.nextLine();
    }
    for(i=0; i <= strings.length; i++){

    for(j=0; j <= queries.length; j++){
          if(queries[j].compareTo(strings[i]) == 0){
                count++;
          }          
    }           
    System.out.println(count);
    count = 0;
    }
      }
}

/* 
Output : ~ no response on stdout ~
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Solution.main(Solution.java:22)
*/

Solution

  • When you call s.nextInt(), it only reads up until the end of the integer, leaving the new line in the buffer.

    Therefore when you subsequently call nextLine() it reads in the new line between the 4 and the aba.

    To work around this, after calling nextInt(), call nextLine() to move the buffer past the new line.

    Scanner s = new Scanner(System.in);
    int n = s.nextInt();
    String strings[] = new String[n];
     int count = 0;
    int i,j=0;
    s.nextLine();
    for(int a=0 ; a<n ; a++){
        strings[a] = s.nextLine();
    }
    

    and

    int q = s.nextInt();
    String queries[] = new String[q];
    s.nextLine();
    for(int b=0; b<q;b++){
        queries[b] = s.nextLine();
    }
    

    Edit:

    Additionally, in the final for loop, you have put:

    for(j=0; j <= queries.length; j++){

    but this will result in an IndexOutOfBoundsException. You actually want:

    for(j=0; j < queries.length; j++){

    Edit 2:

    The previous edit applies to both for-loops at the end - you want the limit term of the for loop to be i<strings.length and j<queries.length respectively.

    Also, to achieve the expected output, the order of the for-loops should be reversed:

    for(j=0; j < queries.length; j++){
        int count = 0;
        for(i=0; i < strings.length; i++) {
            if (queries[j].compareTo(strings[i] == 0)) {
                count++;
            }
        }
        System.out.orintln(count);
    }