I've an ArrayIndexOutOfBounds
error here but I have no idea where abouts it is. In the code I am creating an array where the user inputs the size. the array is a string and the imputs are then all words. Then the user is asked to search the array for a word and see how many times it is there.
Can anyone help?
import java.util.*;
public class SearchArray {
public static void main (String[]args) {
Scanner scan = new Scanner(System.in);
int row = scan.nextInt();
int col = scan.nextInt();
String search = new String();
String array[][] = new String[row][col];
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
array[i][j] = scan.nextLine();
}
System.out.println(countStrings(array,search));
}
}
public static int countStrings(String[][]array, String search) {
int count = 0;
int row = array.length;
int col = array[0].length;
for(int i = 0; i < col; i++){
for(int j = 0; j < row; j++){
if(array[i][j] == search){
count++;
}
}
}
return count;
}
}