Question:
java how to print on the same line?
Rhmag
2011-10-11 18:41:35 UTC
Can someone please help me with this? I dont have any problems with my code, I'm just trying to change the way my output is printed.


My output comes out as:

You can remove these colors: red
yellow
green


How do I make the output on the same line? Like this:

You can remove these colors: red yellow green



Thanks!




part of code:

if(day < 4 && day != 0) {
System.out.print("You can remove these colors:");
}

if (day < 4 && day%3 != 0) {
System.out.println(" red");
}

if(day < 4 && day%2 == 0 && day%5 != 0 && day > 0) {
System.out.println(" yellow");
}

if (day < 4 && day%2 == 0 && day != 0 && day > 0) {
System.out.println(" green");
Four answers:
Richard
2011-10-11 18:44:37 UTC
just use print then println at the end to output line



if (day < 4 && day%3 != 0) {

System.out.print(" red");

}



if(day < 4 && day%2 == 0 && day%5 != 0 && day > 0) {

System.out.print(" yellow");

}



if (day < 4 && day%2 == 0 && day != 0 && day > 0) {

System.out.print(" green");

}



println();
buczek
2016-12-07 06:28:36 UTC
before everything, you do no longer ought to do 5 loops to tug this off. 2nd, making use of println is merely as functional. type FlowControl { public static void important(String[] args) { for (int i = a million; i <= 50; i++) { // Print the present type with an area device.out.println(i + " "); // confirm if line wreck would desire to be further String thisNum = i.toString(); if (thisNum.endsWith("0")) { device.out.println("n"); } } device.go out(0); } } you could locate what I did. Convert the loop administration variable i to a string (i'm uncertain if my syntax is right with .toString, yet i'm tremendously specific that's) and if it ends with 0 (that's a nil) then upload a line wreck. Else, the numbers stay further to the at the instant line. this would desire to help.
McFate
2011-10-11 18:43:13 UTC
Use System.out.print() instead of println(), for your intermediate prints. Then use "println()" for your final one for the line, only.



println() prints whatever you specify PLUS an end-of-line character. print() does exactly the same printing except it doesn't end a line.



If your last print ("green") is optional and might not get printed, then you can call println() without any arguments (in a position where it's always executed) to print only an end-of-line character.
Ketan Soni
2011-10-11 19:06:29 UTC
In your source code find & replace "println" by "print".


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...