Kurumi Tokisaki
Kurumi Tokisaki

Reputation: 13

2D Java array- how to input values

I am a 1st year student, and I don't know what to do here. I have input values but I keep getting 0 as a result. I am expecting output like this:

0000000000

1111111111

2222222222

3333333333

4444444444

but it keeps on giving me 0.

Here is my code so far:

    String [][]array2D = new String[5][10];

    for(int r=0; r<array2D.length; r++)
    {
        for(int b=0; b<array2D[r].length; b++)
        {
            System.out.print(array2D[r][b]);    
        }
        System.out.println();
    }

    System.out.println("Prepared By: Mark Vincent D. Yap");
}

Upvotes: 0

Views: 50

Answers (2)

Laxman Kumar
Laxman Kumar

Reputation: 11

you just forgot to put inputs (filling array values) in your., rest of fine. See below is your code in which I just added array value input statement.

String [][]array2D = new String[5][10];

for(int r=0; r<array2D.length; r++)
{
    for(int b=0; b<array2D[r].length; b++)
    {
        array2d[r][b]=String.values(r);  
        System.out.print(array2D[r][b]);    
    }
    System.out.println();
}

System.out.println("Prepared By: Mark Vincent D. Yap");

}

Upvotes: 0

Diligent Key Presser
Diligent Key Presser

Reputation: 4253

This gives the desired result:

    String [][]array2D = new String[5][10];

    for(int r=0; r<array2D.length; r++)
    {
        for(int b=0; b<array2D[r].length; b++)
        {
            array2D[r][b] = Integer.toString(r);
        }
        System.out.println();
    }

    for(int r=0; r<array2D.length; r++)
    {
        for(int b=0; b<array2D[r].length; b++)
        {
            System.out.print(array2D[r][b]);
        }
        System.out.println();
    }

    System.out.println("Prepared By: Diligent Key Presser");

Hope you will take advance of this, not just copy and paste.

Upvotes: 1

Related Questions