1,105,352 Community Members

Printing array results in a single line.

Member Avatar
MrDiaz
Junior Poster in Training
59 posts since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi, I'm wondering how I can accomplish this.

Suppose I have the following array

String[] randomText = {"hi", "hello", "hey there"}

;

If I want to print the array I only know how to do so line by line.

for (int num = 0; num < randomText.length; num++){
	System.out.println(randomText[num]);
}

Can someone point me in the right direction?

Member Avatar
peter_budo
Code tags enforcer
7,935 posts since Dec 2004
Reputation Points: 2,502 [?]
Q&As Helped to Solve: 1,028 [?]
Skill Endorsements: 66 [?]
Moderator
Featured
 
0
 
System.out.print("I will print on same line");
Member Avatar
josh2
Light Poster
25 posts since May 2007
Reputation Points: 9 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Yeah, what exactly do you mean? System.out.println() will print out each element on a new line.. if you use System.out.print(), then you can print each element on the same line..

for (int i = 0; i < randomText.length - 1; i++)
	System.out.print(randomText[i] + " ");
System.out.print(randomText[randomText.length - 1]);
//Prints out randomText[] on one line, delimited by spaces.
Member Avatar
MrDiaz
Junior Poster in Training
59 posts since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

It was the System.out.print statement I guess, I missed that hehe.

Thanks.

Question Answered as of 5 Years Ago by peter_budo and josh2
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article