0

I want to arrange a random input string array to a table look use a loop and if functions or something easier.

There are smallest, small, medium, big, and biggest. And the string are duplicate x5 each

The array is:

    String cow[][] = new String[5][5];

    cow[0][0] = "big";
    cow[0][1] = "smallest";
    cow[0][2] = "small";
    cow[0][3] = "medium";
    cow[0][4] = "biggest";

    cow[1][0] = "smallest";
    cow[1][1] = "biggest";
    cow[1][2] = "medium";
    cow[1][3] = "small";
    cow[1][4] = "big";

    cow[2][0] = "medium";
    cow[2][1] = "biggest";
    cow[2][2] = "big";
    cow[2][3] = "smallest";
    cow[2][4] = "small";

    cow[3][0] = "small";
    cow[3][1] = "big";
    cow[3][2] = "smallest";
    cow[3][3] = "medium";
    cow[3][4] = "biggest";

    cow[4][0] = "biggest";
    cow[4][1] = "medium";
    cow[4][2] = "big";
    cow[4][3] = "small";
    cow[4][4] = "smallest";

My code for arrange the array:

for (int j = 0; j < cow.length; j++) {
    for (int i = 0; i < cow[j].length; i++) {
        if (cow[i][j] == "smallest") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "small") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "medium") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "big") {
            System.out.print("| " + cow[i][j] + " |");
        } else if (cow[i][j] == "biggest") {
            System.out.print("| " + cow[i][j] + " |");
        }
    }
    System.out.println();
}

When I run the code, I only get
smallest |
smallest |
smallest |
smallest |
smallest |

I think its because the loop didnt re-loop(?) for other index, so the table not 5x5

What I expected:

smallest | small | medium | big | biggest
smallest | small | medium | big | biggest
smallest | small | medium | big | biggest
smallest | small | medium | big | biggest
smallest | small | medium | big | biggest

please kindly teach me, because i very new to java. thanks

  • Possible duplicate of [java Arrays.sort 2d array](https://stackoverflow.com/questions/15452429/java-arrays-sort-2d-array) – Tom Oct 02 '19 at 14:03
  • 2
    Aside from main issue: `if (cow[i][j] == "smallest"){` [How do I compare strings in Java?](https://stackoverflow.com/q/513832) – Pshemo Oct 02 '19 at 14:05
  • Possible duplicate of [Sort an array in Java](https://stackoverflow.com/questions/8938235/sort-an-array-in-java) – pringi Oct 02 '19 at 14:07
  • 1
    As written, this prints the matrix sideways – David Zimmerman Oct 02 '19 at 15:08
  • the possible duplicate is in int, I cant understand it – Mizaki Rion Oct 02 '19 at 15:26
  • Mizaki, if you compare int or String doesn't matter for the general approach on sorting an array. If you don't know how to write a Comparator for your Strings, then consult one of the many tutorials on the Internet on how to create a custom Comparator. – Tom Oct 02 '19 at 16:13

2 Answers2

0

You need to compare two strings using equals method.

if ((cow[i][j]).equals("small")) {
........ }
Habib Mhamadi
  • 729
  • 1
  • 6
  • 15
0

in here we used ArrayList to store every row and Collections class to sort it , then we used StringJoiner for each row to add pipeline between values you can see details in code comments. and remember to import all necessary classes

String cow[][] = new String[5][5];

    cow[0][0] = "big";
    cow[0][1] = "smallest";
    cow[0][2] = "small";
    cow[0][3] = "medium";
    cow[0][4] = "biggest";

    cow[1][0] = "smallest";
    cow[1][1] = "biggest";
    cow[1][2] = "medium";
    cow[1][3] = "small";
    cow[1][4] = "big";

    cow[2][0] = "medium";
    cow[2][1] = "biggest";
    cow[2][2] = "big";
    cow[2][3] = "smallest";
    cow[2][4] = "small";

    cow[3][0] = "small";
    cow[3][1] = "big";
    cow[3][2] = "smallest";
    cow[3][3] = "medium";
    cow[3][4] = "biggest";

    cow[4][0] = "biggest";
    cow[4][1] = "medium";
    cow[4][2] = "big";
    cow[4][3] = "small";
    cow[4][4] = "smallest";

    for (int i = 0; i < cow.length; i++) {
        List<String> list = new ArrayList<>(); //list for saving each row
        for (int j = 0; j < cow[i].length; j++) {

            list.add(cow[i][j]);

        }
        Collections.sort(list); // sort array
        Collections.reverse(list); // reverse array just for better order
        String big = list.get(4);  // since the biggest comes befor big
        list.set(4, list.get(3)); // we need to change them
        list.set(3, big);

        for(int x=0;x<list.size();x++){
if(x<list.size()-1){  System.out.print(list.get(x)+" | "); }
else { System.out.println(list.get(x)); } }

    } // end of outer loop
Habib Mhamadi
  • 729
  • 1
  • 6
  • 15