1

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
Rakesh
  • 4,264
  • 4
  • 32
  • 58
  • Why can't you use \n? Because it's platform dependant or...? – Perry Monschau Mar 19 '12 at 12:52
  • 1
    possible duplicate of [Java: How do I get a platform independent new line character?](http://stackoverflow.com/questions/207947/java-how-do-i-get-a-platform-independent-new-line-character) – oers Mar 19 '12 at 12:53
  • 5
    When interviews are turned into trivia contests, everyone loses. Just what is knowing that particular piece of information supposed to indicate one way or the other? I'm not criticizing you for wanting to know - you didn't create the question - but I'm afraid my own reaction is "I don't know and I don't care". Are carriage return/linefeed characters allowed? You could put those between each numbers and meet the (stupid) requirements as given. – arcy Mar 19 '12 at 12:54
  • @PerryMonschau, I don't know why I can't use `\n`, was wondering if it is possible to print that without using `\n`. – Rakesh Mar 19 '12 at 12:56
  • 1
    @rcook, its not about meeting the (stupid) requirement. I wanted to know if it there is a way to do this. But *"I don't know and I don't care"* reaction doesn't give any answers, does it ? – Rakesh Mar 19 '12 at 12:58
  • @MisterSmith, printing multiple lines using **single** `println()` – Rakesh Mar 19 '12 at 13:00
  • Perhaps the point of the question is to see if you will stand up and say it is stupid. println includes a new line, you can use recursion to simulate a loop. – Martin Spamer Mar 19 '12 at 13:02
  • I was reacting to this as an interview question -- it appears to be a trivia question. It does not, in my opinion, help evaluate whether someone knows how to program in Java, whether they can answer it or not. It is also incomplete; are you blocked from executing println more than once, or just from having it more than once in the source? Can you use the system's platform-independent newline? I'm happy to give answers to substantive questions, or even trivia questions, but it this sort of thing should not be used to evaluate job candidates. – arcy Mar 19 '12 at 16:04

9 Answers9

5

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.

Zack Marrapese
  • 12,072
  • 9
  • 51
  • 69
  • Technically, recursive functions _are_ loops, because you can think of them as: while (!satisfied) {satisfied = something(argtuple); callStack.push(argtuple); argtuple = somethingElse(argtuple)}; while ((argtuple = callstack.pop()) != null) {wrapupStuff(argtuple)}; – root Jun 27 '13 at 20:03
  • @Zack Oh, I know my comment's not relevant to the question. That's why my comment is: (a) not in response to the question, but the answer, and (b) a comment, not a proposed answer. It just feels strange that this probably _is_ a good answer to the question. Basically, this answer being accepted means the question can be restated 'How could you obfuscate a call to println?', which is a very silly question. No disrespect to your answer, though. It's a solid answer. – root Jun 28 '13 at 04:36
4

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!

SimonSez
  • 7,399
  • 1
  • 30
  • 35
2

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');
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

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));
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
1

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++));
}
Thomas
  • 5,074
  • 1
  • 16
  • 12
1

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));
wattostudios
  • 8,666
  • 13
  • 43
  • 57
0

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).

erakitin
  • 11,437
  • 5
  • 44
  • 49
root
  • 471
  • 5
  • 18
0

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);
}
Perry Monschau
  • 883
  • 8
  • 22
0

The ASCII value of new Line is 10. So use this

char line = 10;
System.out.print("1" + line + "2" + line ......);
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125