/    Sign up×
Community /Pin to ProfileBookmark

Hi
I have a simple Java question.

I want to create a Java Application that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space. Write the Java application using the following methods:

Using one (1) System.out statement.

This I got with no problem

Using four (4) System.out statements
This one I cant figure out….and I read the chapter 4 times.
no clue.
Help
Thanks.

to post a comment
Full-stack Developer

1 Comments(s)

Copy linkTweet thisAlerts:
@spufiFeb 19.2003 — What you need to do is not include println which drops the next statement down to the next line. Use print instead and it will remain on the same line. See code below for having it in one System.out and for 4 System.out statements. I'm also curious as to what book you are using.

public class Numbers

{

public static void main(String[] args)

{

int numOne = 1;

int numTwo = 2;

int numThree = 3;

int numFour = 4;

System.out.println(numOne + " " + numTwo + " " + numThree + " " + numFour);
System.out.print(numOne + " ");
System.out.print(numTwo + " ");
System.out.print(numThree + " ");
System.out.print(numFour);
}

}
×

Success!

Help @zacaritex spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...