0

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;   
    } 
}
Kaushal28
  • 4,823
  • 4
  • 30
  • 57

2 Answers2

2

First of all use scan.next() instead of scan.nextLine().

array[i][j] = scan.next();

see this for difference

And String is object and they are always same. So don't compare it using ==. Use .equals() to compare two strings.

array[i][j].equals(search);

==tests for reference equality (whether they are the same object).

.equals() tests for value equality (whether they are logically "equal").

And yeah, as mentioned is comments by others, you have swapped rows and columns in last nested for loop.

It should be:

 for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            if(array[i][j].equals(search)){
                count++;
            }
        }
    }

Hope this helps :)

EDIT:

Keep this line out of your nested for loops:

System.out.println(countStrings(array,search));

and also use array[0].length to get rows and array[1].length to get length of col.

So the whole code will look like:

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();
    System.out.println("Scan the string to be searched!");
    String search = scan.next();
    String array[][] = new String[row][col];

    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            array[i][j] = scan.next();
        }

    }

    System.out.println(countStrings(array, search));

}   
public static int countStrings(String[][]array, String search)   {
    int count = 0;
    int row = array[0].length;
    int col = array[1].length;
    for(int i = 0; i < row; i++){
        for(int j = 0; j < col; j++){
            if(array[i][j].equals(search)){
                count++;
            }
        }
    }
    return count;   
} 
}
Community
  • 1
  • 1
Kaushal28
  • 4,823
  • 4
  • 30
  • 57
  • Ive only changed these and now whenever i enter n the row and col it doesnt let me finish the array. eg. with a 2x2 array i could only enter 2 words before i got an error. It seems to only allow me to enter as many words as the second digit./ –  Feb 28 '17 at 17:45
  • So if i enter 3 rows and 2 columns, it will only allow 2 words and then i get "java.lang.NullPointerException at SearchArray.countStrings(SearchArray.java:33) at SearchArray.main(SearchArray.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)" –  Feb 28 '17 at 17:51
  • Now its entering the array and wont add anything to search. I have moved the line `String search = new String();` to be after i enter it just above where i call the countStrings method. –  Feb 28 '17 at 18:09
  • where you've scanned `search` string? – Kaushal28 Feb 28 '17 at 18:10
  • amazing! yes. Youve no idea the headache I have had with this today. Thank you! –  Feb 28 '17 at 18:18
  • one more thing, how would i make this say duck = Duck? ignoring upper and lower case –  Feb 28 '17 at 18:20
  • Use this: `s1.equalsIgnoreCase(s2)` @Shauna – Kaushal28 Feb 28 '17 at 18:21
0

Sorry i dont have enough reputation points to comment on your follow up. The reason you cannot fill the array is probably because the scan.nextInt(); doesn't consume the "\n".

you can consume the newline char by changing your code to consume the \n like so: Scanner scan = new Scanner(System.in); int row = scan.nextInt(); int col = scan.nextInt(); scan.nextLine();

Peter Halligan
  • 565
  • 4
  • 14
  • Its still giving the same error. Its really frustrating I've been looking over it all day –  Feb 28 '17 at 17:58