Click here to Skip to main content
11,714,345 members (81,463 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Java array
For my Computer Science lab, I have to make a method that returns a string:

public static String arrayToString(int[] [] a)

a is a two dimensional integer array. The string has to separate each index with a space, and a new line after every row (\n). The rows can't have a space before or after the last value on the row, and the columns can't have a new line before or after the last column. How can I do this? I know it involves a nested for loop but I can't figure out how to code it. Thanks in advance!
Posted 20-Apr-13 16:24pm
ThePhantomUpvoter at 21-Apr-13 5:36am
"how to code it", how to code what? A for loop? A nested for loop? Or you just need the whole thing written for you?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

This is a pretty basic problem. You should get some help from a live person wherever you are a student.

You obviously need 2 nested loops (1 for array row, 1 for column). You want to output each value, providing a space either before or after each element (suggestion: detect the first element in the row and don't print a space), and a new line at the end of each row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 420
1 Maciej Los 229
2 Richard MacCutchan 220
3 OriginalGriff 170
4 F-ES Sitecore 140
0 Sergey Alexandrovich Kryukov 545
1 CHill60 375
2 Maciej Los 324
3 Richard MacCutchan 310
4 OriginalGriff 260

Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 22 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100