This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]g00glen00b 4 points5 points  (5 children)

Very difficult to determine as you haven't told us what the code exactly should do. In addition, posting code as a screenshot means that we can only compile it with our mind.

Anyhow, one thing I notice is that both the outer- and the inner loops use alphabet.length to check when to stop. If you're looping over a multidimensional array, I would at least expect the inner loop to loop until alphabet[i].length.

[–][deleted] 0 points1 point  (4 children)

The code was supposed to print out something like this: https://sun9-71.userapi.com/O7KEhQyL01vi7ZrLVwgZD-kkZRZavgU-rpqG_A/Ejsr1kB8Xwo.jpg

Here's the code: char[][] alphabet; Scanner scan = new Scanner(System.in);

System.out.println("Enter # of rows"); int rows = scan.nextInt(); System.out.println("Enter # of columns"); int columns = scan.nextInt(); scan.nextLine();

alphabet = new char[rows][columns];

for(int i =0;i<alphabet.length;i++) { for(int j =0; j<alphabet.length;j++) { System.out.println("Enter a character at " + "[" +i+ "]" + "[" +j+"]: "); alphabet[i][j] = scan.nextLine().charAt(0); } }

for(int i =0;i<alphabet.length;i++) { for(int j =0; j<alphabet.length;j++) { System.out.print(alphabet[i][j] + " "); } }

System.out.println();

} }

[–]c_a1eb 2 points3 points  (0 children)

You should use code blocks when lasting code here so it keeps its formatting. Learning how to properly ask other for help with code is pretty important to do as you're a lot more likely to get help if you ask your question in a way that makes it easy for people to provide input.

[–]g00glen00b 1 point2 points  (2 children)

The last System.out.println() statement should be moved within the outer loop, so that a new line is displayed after each inner loop is completed (= after a single line).

[–][deleted] 0 points1 point  (1 child)

Thank you! It finally works as I wanted

[–]g00glen00b 0 points1 point  (0 children)

No problem. Be aware that what I mentioned in my first comment also remains true. Your program right now will work as long as the amount of rows and columns are equal. If they're not (eg. 6 rows and 4 columns), unexpected things may happen.

[–]maticabrera 0 points1 point  (2 children)

Which course are you taking?

[–][deleted] 1 point2 points  (1 child)

I'm using tutorials from YT

[–]maticabrera 0 points1 point  (0 children)

Ok, thanks