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;   
    } 
}
3
  • You've swapped row and col in the last nested loops. Commented Feb 28, 2017 at 17:24
  • You shouldn't compare strings by ==. use .equal instead. Commented Feb 28, 2017 at 17:24
  • It isnt actually letting me input everything to the array either Commented Feb 28, 2017 at 17:33

2 Answers 2

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;   
} 
}
Sign up to request clarification or add additional context in comments.

7 Comments

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./
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)"
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.
where you've scanned search string?
amazing! yes. Youve no idea the headache I have had with this today. Thank you!
|
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();

1 Comment

Its still giving the same error. Its really frustrating I've been looking over it all day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.