简体   繁体   中英

Java search 2D string array

I am trying to implement sequential search on a 2D array in java.

I currently have two classes. In the first class, the user enters the data values into the array and inputs the term that he wishes to search (in the array/data values he just entered).

This search is handled by a method (second class) which conducts sequential search and then passes the results back to the first class.

I have tried using this following code, but the sequential search is not working...

File #1 (input file):

static public void s_2d_string () {
            int counter,x;
            counter = 2;

            String[][] sortValues = new String[counter+1][2];

            for (x=0;x<counter;x++) {
                    System.out.print("Enter book name: ");
                sortValues[x][0] = readLine();
                    System.out.print("Enter book author: ");
                sortValues[x][1] = readLine();

            }
            System.out.print("Which column would you like to sort by? 1 or 2? ");
            String sortBystring = readline();
            int sortBy;
            sortBy = Integer.parseint(sortBystring);
            sortBy = sortBy-1;

            System.out.print("Enter search term: ");
            String searchterm = readLine();

            sortValues = s.sort(sortValues,counter, sortBy, searchterm);

            int flagcounter_int = Integer.parseInt(sortValues[0][0]);

            System.out.println(flagcounter_int + " results found.");

            for (x=0;x<flagcounter_int;x++) {
                    System.out.println(sortValues[x+1][0] + ", " + sortValues[x+1][1]);
            }
    }

File #2:

static public String[][] sort (String data[][], int totalNo, int sortBy, String searchterm)  {
        boolean found = false;    
        int flagcounter = 0;
            if (sortBy == 0) {
                    for (int x=0; x<totalNo;x++) {
                            if (searchterm.equals(data[x][0])) {
                                    found = true;
                                    flagcounter = flagcounter+1;
                                    data[flagcounter] = data[x];
                            }
                    }
            }
            if (sortBy == 1) {
                    for (int x=0; x<data.length;x++) {
                            if (searchterm.compareTo(data[x][1]) == 0) {
                                    found = true;
                                    flagcounter = flagcounter+1;
                                    data[flagcounter] = data[x];
                            }
                    }
            }
            String flagcounter_string = Integer.toString(flagcounter);
            data[0][0] = flagcounter_string;
                    return data;
                    }

The specific problem is that if I try to search for term "k" in column "n" and the term "k" appears in the first row (regardless of which column), the search function will list number of rows in the array results found and list as the results: number of rows, k (repeating for how many rows are in the array). This problem does not occur if "k" does not appear in the first row (the search function works perfectly).

How to fix this?

You have a bug: In the sort method, the first for loop (when sortBy == 0 ) terminates when x<totalNo , but it should be x<data.length (like your second loop).

As a general comment, your code is terrible, whether it works or not. I recommend:

  • using Collections instead of arrays wherever possible
  • rather than having two loops, have one loop and simply use sortBy as the index instead of hard-coding the column index as 0 or 1
  • use .equals() instead of .compareTo() == 0
  • use a class to return sort result, rather than cramming data into an array

Not sure where the bug is, you should provide all the code.

But possible causes are

1) You use both for(int x=0; x<totalNo;x++) and for(int x=0; x<data.length;x++) , I would stick to the 2nd way only, then you dont even need the totalNo argument

2) For string comparison you use both searchterm.equals(data[x][0]) and searchterm.compareTo(data[x][1]) == 0 , I would stick to the first way

3) Dont call a search routine 'sort', one would expect a routine called sort to, well, sort..

4) Consider using if (searchterm.equals(data[x][sortBy ])) { because then you dont need to distinguish between the two types of search.

T.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM