Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing 2-dimensional array 1

Status
Not open for further replies.

technoknow

Technical User
Apr 19, 2002
121
US
public class MyArray
{
public static void main(String args[]) throws Exception
{
int i; int j;
String[][] friends =
{ {"Dave", "Henrich", "33"},
{"Jim", "Creighton", "42"},
{"Bob", "Wilson", "47"} };
for (i = 0; i< friends.length; i++)
for (j = 0; j < friends.length; j++)
System.out.println(friends[j]);
}
}

I'm trying to print this array with each row on 1 line like this:
Dave Henrich 33
Jim Creighton 42
Bob Wilson 47
But it comes out like this:
Dave
Henrich
33
Jim etc....
What am I doing wrong?
Thanks,
 
You are using println, so each time you print an item you print a new line.

Code:
for (i = 0; i< friends.length; i++)
{
  for (j = 0; j < friends[i].length; j++)
    System.out.print (friends[i][j] + &quot; &quot;);
  System.out.println ();
}

This code should work.

--
Globos
 
Globos,
Now all of the elements are printed on one line like this:
DaveHenrich33JimCreighton42, etc..
Any other ideas?
Thanks
 
hi

println : print & go next line
print : just print

so add new println statement after inner loop (don't forget { & } )

public class MyArray
{
public static void main(String args[]) throws Exception
{
int i; int j;
String[][] friends =
{ {&quot;Dave&quot;, &quot;Henrich&quot;, &quot;33&quot;},
{&quot;Jim&quot;, &quot;Creighton&quot;, &quot;42&quot;},
{&quot;Bob&quot;, &quot;Wilson&quot;, &quot;47&quot;} };
for (i = 0; i< friends.length; i++) {
for (j = 0; j < friends.length; j++){
System.out.print(friends[j]);
}
System.out.println();
}
}
 
Code:
for (i = 0; i< friends.length; i++)
{
  for (j = 0; j < friends[i].length; j++)
    System.out.print (friends[i][j] + &quot; &quot;);
  System.out.println ();
}

tecknoknow, this code works fine on my PC, with Java 1.4.2_02 installed.

--
Globos
 
But the code from fuadhamidov doesn't work, when executed I get :
Code:
[Ljava.lang.String;@108786b[Ljava.lang.String;@119c082[Ljava.lang.String;@1add2dd
[Ljava.lang.String;@108786b[Ljava.lang.String;@119c082[Ljava.lang.String;@1add2dd
[Ljava.lang.String;@108786b[Ljava.lang.String;@119c082[Ljava.lang.String;@1add2dd

Moreover the inner for loop as a bad end condition
not
Code:
 for (j = 0; j < friends.length; j++)
but
Code:
 for (j = 0; j < friends[i].length; j++)
and the body of this loop doesn't seem to be correct too.


--
Globos
 
>>But the code from fuadhamidov doesn't work, when executed I get :

That's because fuadhamidov did not put his code between [ code ] and [ /code ] so that
Code:
friends[i].length
gets displayed by this forum as :
friends.length

[ i ] means &quot;display in italics&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top