Haider Abdullah
Haider Abdullah

Reputation: 111

Multi-dimensional Array Using Scanner

package practice;

import java.util.*;
import java.util.ArrayList;
import java.util.Iterator;

public class Practice {

public static void main(String[] args){

    Scanner input = new Scanner(System.in);
    StringBuilder output = new StringBuilder();


    System.out.println("Enter the number of rows & columns: ");

    System.out.print("Enter the number of rows: ");
    int row = input.nextInt();
    System.out.print("Enter the number of columns: ");
    int columns = input.nextInt();

    int [][]nums = new int[row][columns];

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            System.out.print("Number " + (j + 1) + ": ");
            nums[i][j] = input.nextInt();
            output.append("\n").append(nums[i][j]);
        }
        System.out.println( " " );

    }

    System.out.println(output);

   }

}

I have a problem with the code shown above, I'm practicing multi-dimensional array. What I want is to make a list of numbers in which they are separated by rows and columns, Example: if I entered 3 in the rows and 4 in the column the output numbers should be arranged like this.

10 20 30 40
50 60 70 80
90 100 101 102

But that problem is the output showing is a long line of continuous numbers. can anyone help me to solve this issue,

Thank you,

Upvotes: 3

Views: 5935

Answers (3)

Arun Sudhakaran
Arun Sudhakaran

Reputation: 2405

Change your nested for loop content as

for (int i = 0; i < row; i++)
{
    for (int j = 0; j < columns; j++)
    {
        System.out.print("Number " + (j + 1) + ": ");
        nums[i][j] = input.nextInt();
        output.append(nums[i][j]).append(" ");
    }
    System.out.println();
    output.append("\n");
}

Upvotes: 0

nikowis
nikowis

Reputation: 103

You might want to add your new line in the outer loop like so :

for (int i = 0; i < row; i++)
{
    for (int j = 0; j < columns; j++)
    {
        System.out.print("Number " + (j + 1) + ": ");
        nums[i][j] = input.nextInt();
        output.append(nums[i][j]);
    } 

    output.append("\n");

}

Upvotes: 0

Michał Szewczyk
Michał Szewczyk

Reputation: 8168

You have to add a new line to your output, when switching to next row:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    StringBuilder output = new StringBuilder();


    System.out.println("Enter the number of rows & columns: ");

    System.out.print("Enter the number of rows: ");
    int row = input.nextInt();
    System.out.print("Enter the number of columns: ");
    int columns = input.nextInt();

    int[][] nums = new int[row][columns];

    for (int i = 0; i < row; i++) {
        for (int j = 0; j < columns; j++) {
            System.out.print("Number " + (j + 1) + ": ");
            nums[i][j] = input.nextInt();
            output.append(" ").append(nums[i][j]);
        }
        output.append("\n");
        System.out.println("\n");

    }

    System.out.println(output);

}

Upvotes: 3

Related Questions