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 variablestrings
. - Each of the next
n
lines contains a stringstrings[a]
. - The next line contains
q
, the size ofqueries
. - Each of the next
q
lines contains a stringqueries[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)
*/