Click here to Skip to main content
11,584,622 members (70,899 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Java arrays
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 OriginalGriff 1,703
1 Sergey Alexandrovich Kryukov 1,267
2 Abhinav S 711
3 Dave Kreskowiak 687
4 Suvendu Shekhar Giri 568

Advertise | Privacy | Mobile
Web04 | 2.8.150707.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