-1

I am trying to run my code line but it shows a minor error. I also make some effort to put something like \ ; " " but it doesn't work.

public static void message() {
    System.out.println("This program surely cannot");
    System.out.println("have any \"errors")"in it");
}

I expect that this method can run, but it shows that:

Error:(9, 48) java: ';' expected.
ruohola
  • 21,987
  • 6
  • 62
  • 97
Hòa Đỗ
  • 37
  • 8
  • 2
    `System.out.println("have any \"errors\")\"in it");` should work, but does not make sure you get what you want as output. – deHaar Jul 22 '19 at 08:35

2 Answers2

6

You need to escape all of the quotes " in the string with backspaces \:

public static void message() {
    System.out.println("This program surely cannot");
    System.out.println("have any \"errors\")\"in it");
}

Otherwise the parser sees that you've ended your string, and will break when there's some random characters after the ) that it doesn't know how to interpret. This is simply a syntax error, and will crash when compiling.

System.out.println("have any \"errors")"in it");
                                     ^^^~~~~~~~~
                                     |||    ^
                                     |||    |
                                     ||| Parser sees these as just random characters
                 String ends here____|||
                                      ||____Here should be a semicolon
                                      |
                                     println( matches this

ruohola
  • 21,987
  • 6
  • 62
  • 97
4
System.out.println("have any \"errors")"in it");

There is a syntax error: you have to escape the quotes:

System.out.println("have any \"errors\")\"in it");