Reputation: 4334
this is one of the interview question. I am supposed to print multiple lines of output on command line, without using the newline(\n
) character in java. I tried googling for this, didn't find appropriate answers. If i am printing 5 numbers, then it should print in the following fashion. But I am not supposed to use the newline character nor loops either. I have to print this using a single println()
statement. Can you give me some ideas ? Thanks !
1
2
3
4
5
Upvotes: 1
Views: 46205
Reputation: 491
ANSI terminal escape codes can do the trick.
Aside: Since System.out
is a PrintStream, it may not be able to support the escape codes.
However, you can define your own println(msg)
function, and make one call to that. Might be cheating, but unless they explicitly say System.out.println
, you're golden (hell, even if they do, you can define your own object named System
in the local scope using a class defined outside your function, give it a field out
with a function println(msg)
and you're still scot-free).
Upvotes: 0
Reputation: 5094
Probably cheating based on the requirements, but technically only 1 println statement and no loops.
public int recursivePrint(int number)
{
if (number >=5 )
return number;
else
System.out.println(recursivePrint(number++));
}
Upvotes: 1
Reputation: 19500
The ASCII value of new Line is 10. So use this
char line = 10;
System.out.print("1" + line + "2" + line ......);
Upvotes: 0
Reputation: 8774
There are many ways to achieve this...
One alternative to using '\n' is to output the byte value for the character. So, an example to print out your list of the numbers 1-5 in your example...
char line = (char)10;
System.out.println("1" + line+ "2" + line+ "3" + line + "4" + line+ "5");
You could also build a byte[] array or char[] array and output that...
char line = (char)10;
char[] output = new char[9]{'1',line,'2',line,'3',line,'4',line,'5'};
System.out.println(new String(output));
Upvotes: 1
Reputation: 901
No loops, 1 println call, +flexibility:
public static void main (String[] args) {
print(5);
}
final String newLine = System.getProperty("line.separator");
public void print(int fin) {
System.out.println(printRec("",1,fin));
}
private String printRec(String s, int start, int fin) {
if(start > fin)
return s;
s += start + newLine;
return printRec(s, start+1, fin);
}
Upvotes: 0
Reputation: 786001
One way is this: Platform Independent
final String EOL = System.getProperty("line.separator");
System.out.println('1' + EOL + '2' + EOL + '3' + EOL + '4' + EOL + '5');
This is Platform Dependent
char eol = (char) 13;
System.out.println("" + '1' + eol + '2' + eol + '3' + eol + '4');
Upvotes: 2
Reputation: 28189
Ok, now I think I understand your question. What about this?
println(String.format("%d%n%d%n%d%n%d%n%d%n", 1, 2, 3, 4, 5));
Upvotes: 2
Reputation: 7769
If you're just not allowed of using \n
and println()
then you can get the systems line.separator
, e.g.
String h = "Hello" + System.getProperty("line.separator") + "World!"
Hope this helped, have Fun!
Upvotes: 4
Reputation: 12091
You can do it recursively:
public void foo(int currNum) {
if (currNum > 5)
return;
println(currNum);
foo(currNum + 1);
}
Then you are only using a single println
and you aren't using a for or while loop.
Upvotes: 5