1

When I'm writing out my string, it tells me the string literal is not properly closed, but it looks like it is..? and then if i take out the /|\ the error moves down 2 lines to the legs. I researched and i cant seem to know what the problem is...

public static void printMan(int badGuesses) {
    String[] man = new String[];
             man={"______",
                  "|    |",
                  "|    o",
                  "|    |",
                  "|   /|\", //it tells me that i need to insert missing quote
                  "|    |",
                  "|   / \"
                 };
    int counter = 0;
    while (counter < badGuesses) {
        System.out.println(man[counter]);
    }
bgschreff
  • 23
  • 5

5 Answers5

9

Looks like you got an escape character

\ Is an escape character, you need to escape it too in this scenario. Otherwise, you'd get an unterminated string. \" means the actual character " rather than the start or an end of a string.

If you want the actual character \ you need to escape it too: \\

 String[] man = new String[]{
              "|    |",
              "|    o",
              "|    |",
              "|   /|\\",  \\<- note the extra \
              "|    |",
              "|   / \\"   \\<- note the extra \ here too
             };

The official Java Tutorial

See the section on escape sequences in the official java tutorial:

A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences (me: table in link)

The language specification

This is of course in line with the language specification:

StringLiteral:
   "StringCharacters"

  StringCharacters:
   StringCharacter
   | StringCharacters StringCharacter

  StringCharacter:
   InputCharacter but not " or \
   | EscapeSequence

   
EscapeSequence:
    \ b    
    \ t   
    \ n   
    \ f   
    \ r    
    \ "   
    \ '  
    \ \  < -  **THIS IS THE ONE YOU HAD**
    OctalEscape   /* \u0000 to \u00ff: from octal value */

(related question)

A broader view

Wikipedia has an interesting article on escape characters too:

In computing and telecommunication, an escape character is a character which invokes an alternative interpretation on subsequent characters in a character sequence. An escape character is a particular case of metacharacters. Generally, the judgement of whether something is an escape character or not depends on context.

Noting:

C, C++, Java, and Ruby all allow exactly the same two backslash escape styles.

Here is another related question here on SO about escaping strings.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • Now that's a good opportunity to learn about *community wiki*. Why this answer is a one? :D – Maroun Jun 23 '13 at 18:23
  • @MarounMaroun to avoid gaining rep that was percieved as undeserved by OP – John Dvorak Jun 23 '13 at 18:24
  • 2
    @MarounMaroun It's a trivial question, as such I did not want rep for it. On the other hand it's something a lot of users know about so it got a lot of edits from a lot of different users fast - doing this allowed us to produce a fast high quality answer. – Benjamin Gruenbaum Jun 23 '13 at 18:25
2

This is because you are using an excape character.. \" causes " to be considered, add \\"

man={"______",
          "|    |",
          "|    o",
          "|    |",
          "|   /|\\",  \\ and extra slash here
          "|    |",
          "|   / \\"   \\ and here
         };
Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
2

You also declare bad the array, if you use initializater must be in this way cause if not you have to provide size to the array.

String[] man = new String[]{"______",
             "|    |",
             "|    o",
             "|    |",
             "|   /|\\",
             "|    |",
             "|   / \\"
            };
nachokk
  • 14,363
  • 4
  • 24
  • 53
0

\ is an escape character. You have to escape it again, so the last line should look like:

"|   / \\"
darijan
  • 9,725
  • 25
  • 38
0

public static void printMan(int badGuesses) {

String[] man = new String[];
         man={"______",
              "|    |",
              "|    o",
              "|    |",
              "|   /|\", //it tells me that i need to insert missing quote
              "|    |",
              "|   / \"
             };
int counter = 0;
while (counter < badGuesses) {
    System.out.println(man[counter]);
}

Suggestion: 1. Add one extra \ before each \ you used in code.

  1. Its a good practice to initialize in the declaration when you are initializing the array anonymously. Like String []man=new String[]{"_","| |"};
Mahmud Hoque
  • 11
  • 1
  • 5