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
8
  • Why can't you use \n? Because it's platform dependant or...? Commented Mar 19, 2012 at 12:52
  • 1
    possible duplicate of Java: How do I get a platform independent new line character? Commented Mar 19, 2012 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. Commented Mar 19, 2012 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. Commented Mar 19, 2012 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 ? Commented Mar 19, 2012 at 12:58

9 Answers 9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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)};
@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.
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!

1 Comment

your answer is also helpful. Thanks.
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));

Comments

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');

Comments

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

Comments

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++));
}

1 Comment

Whoops! Is this really recursive?
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);
}

Comments

0

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

char line = 10;
System.out.print("1" + line + "2" + line ......);

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.