3
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,

2
  • You want to append \n after every forth entry. Commented Jan 18, 2017 at 12:41
  • Thank you for your immediate respond, but can you sho me how am I gonna suppose to do that? Commented Jan 18, 2017 at 12:43

3 Answers 3

3

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);

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the immediate respond I really appreciate your assistance. God bless you and your family.
0

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");

}

1 Comment

Thank you for your help I really appreciate it.
0

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");
}

1 Comment

I really appreciate your assistance thank you very much, until next time brother.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.