-1

My goal is to print a text line break in java, something like this:

System.out.println("----------------");
System.out.println("1. Name");
System.out.println("2. Age");
System.out.printnln("---------------");

Is there a special way to print those -------- lines? The line length doesn't matter.

VeggieVeggie
  • 15
  • 1
  • 1
  • 2
  • This is interesting for you, also: http://stackoverflow.com/questions/15433188/r-n-r-n-what-is-the-difference-between-them – Jose Luis Apr 09 '16 at 08:18
  • 1
    What does printing a line of dashes have to do with printing line breaks? You're already printing a line break every time you call `println()`. If you want a *blank* line, which isn't stated, just call `println()` with no arguments. – user207421 Apr 09 '16 at 08:28

3 Answers3

2

I'd use a helper function:

String dashedLine()
{
    StringBuilder sb = new StringBuilder(20);
    for(int n = 0; n < 20; ++n)
        sb.append('-');
    sb.append(System.lineSeparator());
    return sb.toString();
}

and then:

System.out.print(dashedLine());
IceFire
  • 4,016
  • 2
  • 31
  • 51
0

I don't think there is a way to print the '-------' other than typing it, but what you can do is create your own method to print line breaks for future references like :-

class TextBreak {
  void dash(int n) {
    StringBuilder sb = new StringBuilder();
    while (n > 0) {
      sb.append("-"); // can be changed to print dash with multiples of number
      n--;
    }
    System.out.println(sb);
  }
  void dash() {  // For default length for line break
    int n = 5;
    StringBuilder sb = new StringBuilder();
    while(n > 0) {
      sb.append("-");
      n--;
    }
    System.out.println(sb);
  }
}

Then you can use it for future references by creating an object of the class TextBreak and using the method dash (with or without length arg) to have your line break. Like :-

class LineBreak {
  public static void main(String args[]) {
    TextBreak tb = new TextBreak();
    tb.dash(4);  // With length argument
    tb.dash();   // Without argument
  }
}
-1

Use the following code

System.out.println("----------------\n1. Name\n2. Age\n---------------");
Geeth Lochana
  • 164
  • 13