-3

Hello i am a noob java programmer.I have written two codes the first one works fin e and there is no error however in the second code there are lots of error.Can someone explain why the white spaces are not acknowleged in the second code.Also is there any other way of writing this code without using System.out.println multiple times? code1[with errors]

 public class letter
    {
     public static void main(String[] args)
    {
      System.out.println("+----------------------------------------------------+
                       |                                           ###      |
                       |                                           ##       |
                       |                                           #        |
                       |                                                    |
                       |                                                    |
                       |                                                    |
                       |                                                    |
                       |                                 Bill gates         |
                       |                                 Microsoft 1 way    |
                       |                                 Redmond,WA 98603   |
                       |                                                    |
                       +----------------------------------------------------+");
    }
}

code2[errorless]

  public class Lettertoyourself
{
  public static void main(String[] args)
  {
   System.out.println("+----------------------------------------------------+");
   System.out.println("|                                           ###      |");
   System.out.println("|                                           ##       |");
   System.out.println("|                                           #        |");
   System.out.println("|                                                    |");
   System.out.println("|                                                    |");
   System.out.println("|                                                    |");
   System.out.println("|                                                    |");
   System.out.println("|                                 Bill gates         |");
   System.out.println("|                                 Microsoft 1 way    |");
   System.out.println("|                                 Redmond,WA 98603   |");
   System.out.println("|                                                    |");
   System.out.println("+----------------------------------------------------+");
    }
}
pompeii
  • 1
  • 1

2 Answers2

1

Java doesn't support multi-line strings.

You need to put in the newlines yourself:

System.out.println("  line 1\n  line 2\n  line 3");

or, if you think it more readable:

System.out.println("  line 1\n" +
                   "  line 2\n" +
                   "  line 3");

or:

System.out.println(
    Stream.of(
        "  line 1",
        "  line 2",
        "  line 3").collect(joining("\n"))));

You can replace "\n" with System.lineSeparator() if you want; but I would only really suggest doing that in the third of the styles shown here, as otherwise it will seriously harm the readability of the code.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0
public static void main(String arg[]) {


    System.out.println("+--------------------------------------------+"
            + "|                                                     |"
            + "                                                     ");

}
}

you just need to add + " " for every line , i dont know what u mean by multip system.println but yes you can store the lines on a String var , and then print all of them using one system.print ln