1

I have trouble reading the elements inside a 2d array.

To contextualize, the user delivers an input like this:

7  
1
2
3 1 2
4 3
5 4
6 3
7 5 6

Where the first digit is the number of next inputs (n in my code). And every next first digit is a course, and the digits that follow it are it's requirements (courses too). For example, course 3 needs courses 1 and 2 approved.

I need to count the occurrence of a String inside a 2D array but not considering the first element of each array (list1[i][0]). Also the first row list1[0] is not filled to make index call easier (that's why my function starts with i=1). The problem is that it won't recognize the items that are inside the 2d array, so the count returns always 0. This is my function

static int count(String[][] list, String s, int n) { //n is the number of rows
    int x=0;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<list[i].length;j++) {
            if (list[i][j]==s) {
                x++;
            }
        }
    }
    return x;
}

The function actually works for an array defined like this:

String[][] list1= {{null},{"1"},{"2"},{"3","1","2"},{"4","3"},{"5","4"},{"6","3"},{"7","5","6"}};

Where, for example, count(list1,"1",7) returns 1 and count(list1, "3",7) returns 2.

But for the array where the inputs are saved as strings (list2), supposedly just like list1, the count returns 0.

The way I initialized the 2D array list2:

 String[][] list2 = new String[n+1][];
    String[] L1 = new String[n+1]; //just an auxiliary array
    for (int j=1; j<=n; j++) {
                list2[j] = new String[n+1];
                L1[j] = sc.nextLine();
                list2[j] = L1[j].split(" ");
            }

The weird thing is that if a call an array or an element of list2, it returns what is supposed to (for example, using the input above, list2[3]=["3","1","2"]) but the function just doesn't work. I had the same problem with another homework, and it was related to adding arrays to another array, I couldn't solve it so I changed the way the inputs were saved, but now due to restrictions I have no other choice.

Any help will be appreciated

3 Answers3

2

At Java == means exact same object for object comparison. For String object there is method equals, overrides the Object class equals() method implementation.

equals method returns true if object is a String, not null and represents the same sequence of characters.

static int count(String[][] list, String s, int n) { //n is the number of rows
    int x=0;
    for (int i=1;i<=n;i++) {
        for (int j=1;j<list[i].length;j++) {
            if (list[i][j].equals(s)) {
                x++;
            }
        }
    }
    return x;
}

If equality is not case sensitive use if (list[i][j].equalsIgnoreCase(s))

Erçin Akçay
  • 1,383
  • 4
  • 18
  • 34
1

2 problems:

1: arrays are 0-indexed. Start your loops at 0, not 1
2: use .equals() to compare strings. Never use == for objects unless you know exactly what you're doing

Benjamin Urquhart
  • 1,626
  • 12
  • 18
0

Try using .equals() to compare the value in the matrix to the string s instead of the == operator