-3

Good day,

I'm new to Java and have been practicing with Nested Loop iterations. I was wondering if there is any simpler way on how to do this? Especially on the part where I had to put this [j+1].

String[][] entry = {{"010", "John", "Male", "21"},
                {"011", "Mary", "Female", "25"},
                {"012", "Joseph", "Male", "24"},
                {"013", "Peter", "Male", "22"}};


for ( int i = 0; i < entry.length; i++) {

    for ( int j = 0; j < entry[i].length;) {
        System.out.print("ID: " + entry[i][j] + "\n");
        System.out.print("Name: " + entry[i][j+1] + "\n");
        System.out.print("Gender: " + entry[i][j+2] + "\n");
        System.out.print("Age: " + entry[i][j+3] + "\n");
        System.out.println(""); 
        break;
    }           
}

Expected output is like this:

ID: 010
Name: John
Gender: Male
Age: 21

ID: 011
Name: Mary
Gender: Female
Age: 25

ID: 012
Name: Joseph
Gender: Male
Age: 24

ID: 013
Name: Peter
Gender: Male
Age: 22

Your help is highly appreciated!

1
  • 1
    By the way, this kind of data structure is better represented by writing a custom class. Writing a custom class is now much easier in Java 16 with the new records feature. Commented Jun 1, 2021 at 2:15

2 Answers 2

2

Erm... You can delete your inner loop, as you are breaking in the first iteration, hence not looping at all.

String[][] entry = { {"010", "John", "Male", "21"},
                     {"011", "Mary", "Female", "25"},
                     {"012", "Joseph", "Male", "24"},
                     {"013", "Peter", "Male", "22"} };


for (int i = 0; i < entry.length; i++) {
    System.out.print("ID: " + entry[i][0] + "\n");
    System.out.print("Name: " + entry[i][1] + "\n");
    System.out.print("Gender: " + entry[i][2] + "\n");
    System.out.print("Age: " + entry[i][3] + "\n");
    System.out.println("");       
}

EDIT: If nested loop is mandatory, you can achieve the same output like so:

String[] type = { "ID", "Name", "Gender", "Age" };

String[][] entry = { {"010", "John", "Male", "21"},
                     {"011", "Mary", "Female", "25"},
                     {"012", "Joseph", "Male", "24"},
                     {"013", "Peter", "Male", "22"} };


for (int i = 0; i < entry.length; i++) {
    for (int j = 0; j < entry[i].length; j++) {
        System.out.println(type[j] + ": " + entry[i][j]);
    }
    System.out.println("");       
}

But, I'd advise against this.

3
  • Hello, thank you for your prompt reply. Yes, that does work. However, I'm trying to use nested loop for this. Just edited that part, sorry.
    – shoshinsha
    Commented Jun 1, 2021 at 2:08
  • Well, you can't use a nested loop without extracting the first bits (ID, Name, Gender, Age). See the updated answer for a crude way of doing it, which, I wouldn't recommend.
    – Doga Oruc
    Commented Jun 1, 2021 at 2:11
  • @shoshinsha You aren't using a real nested loop. You start an inner loop, but you assume that j == 0, and before the loop ends, you break out of it unconditionally. So the inner loop could just have been replaced with int j = 0; followed by your print statements.
    – NomadMaker
    Commented Jun 1, 2021 at 2:36
0

I think this is what you are looking for:

String[][] entry = {{"010", "John", "Male", "21"},
                {"011", "Mary", "Female", "25"},
                {"012", "Joseph", "Male", "24"},
                {"013", "Peter", "Male", "22"}};

String[] label = {"ID", "Name", "Gender", "Age"};

for (int i = 0; i < entry.length; i++) {
    for (int j = 0; j < entry[i].length;) {
        System.out.println(label[j] + ": " + entry[i][j]);
    } 
    System.out.println(); 
}

I have removed the break. It shouldn't have been present in the original code either.

It is better to use println than to append a newline. (Appending \n is not portable. The correct line separator to use is OS dependent. There ways to append the correct line separator, but it is simpler to let println take care of it.)

We could also use printf rather than concatenating the strings explicitly.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.