user6267046
user6267046

Reputation:

Linear Search Array and String

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;   
    } 
}

Upvotes: 0

Views: 429

Answers (2)

Kaushal28
Kaushal28

Reputation: 5557

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;   
} 
}

Upvotes: 2

Peter Halligan
Peter Halligan

Reputation: 692

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();

Upvotes: 0

Related Questions