1

My loop for the user input of a 2d array looks like this:

 for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ System.out.println("Enter the value"); int value = s.nextInt(); inArray[i][j] = value; } } 

The x and y values are imported from user and used to construct an array of size = new int[y][x]

It works fine when the x value is greater than the y value but when the other way around it doesn't output the right results

For example when I input a 5x5 array I get

1,2,3,4,5 6,7,8,9,1 2,3,4,5,6 7,8,9,1,2 3,4,5,6,7 

But if I try a 3x4 array I get this

1,2,34 5,6,78 9,1,23 

The values 34, 78 and 23 are meant to be separate in its own column like this:

1,2,3,4 5,6,7,8 9,1,2,3 

1 Answer 1

1

You can have a look @ below code:

public class Laptop { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int[][] inArray = new int[10][10]; for(int i = 0; i < 3; i++){ for(int j = 0; j < 5; j++){ System.out.println("Enter the value"); int value = scan.nextInt(); inArray[i][j] = value; } } for(int i = 0; i < 3; i++){ for(int j = 0; j < 5; j++){ System.out.print(inArray[i][j] + " "); } System.out.println(); } } } 

The Output i am getting :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
Sign up to request clarification or add additional context in comments.

1 Comment

The error wasn't actually with my code, it was with my 2d array to csv code, thanks for helping me realizing that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.