1

I am starting to make a hangman game in java.
I want to be able to call my method states based on if the user gets a character wrong or not.
I am testing all of the states to make sure they display correctly.
State 1 and 2 display correctly, but not 3-5.

Here is the code:

public void state_1() {
        System.out.println(" \n"
                + " ------\n"
                + "|      |\n"
                + "|      O\n"
                + "|\n"
                + "|\n"
                + "|\n"
                + "|\n"
                + "|\n"
                + "|\n"
                + "|");
        System.out.println("\n"
                + "");
    }
    public void state_2() {
        System.out.println(" \n"
                + " ------\n"
                + "|      |\n"
                + "|      O\n"
                + "|      |\n"
                + "|      |\n"
                + "|      |\n"
                + "|\n"
                + "|\n"
                + "|\n"
                + "|");
        System.out.println("\n"
                + "");
    }
    public void state_3() {
        System.out.println(" \n"
                + " ------\n"
                + "|      |\n"
                + "|      O\n"
                + "|      |\n"
                + "|      |\n"
                + "|      |\n"
                + "|      |\n"
                + "|     / \\n"
                + "|    /   \\n"
                + "|   /     \\");
        System.out.println("\n"
                + "");
    }
    public void state_4() {
        System.out.println(" \n"
                + " ------\n"
                + "|      |\n"
                + "|      O  /\n"
                + "|      | /\n"
                + "|      |/\n"
                + "|      |\n"
                + "|      |\n"
                + "|     / \\n"
                + "|    /   \\n"
                + "|   /     \\");
        System.out.println("\n"
                + "");
    }
    public void state_5() {
        System.out.println(" \n"
                + " ------\n"
                + "|      |\n"
                + "|   \\  O  /\n"
                + "|    \\ | /\n"
                + "|     \\|/\n"
                + "|      |\n"
                + "|      |\n"
                + "|     / \\n"
                + "|    /   \\n"
                + "|   /     \\");
        System.out.println("\n"
                + "");
    }

Here is the weird output:

 ------
|      |
|      O
|
|
|
|
|
|
|



 ------
|      |
|      O
|      |
|      |
|      |
|
|
|
|



 ------
|      |
|      O
|      |
|      |
|      |
|      |
|     / \n|    /   \n|   /     \



 ------
|      |
|      O  /
|      | /
|      |/
|      |
|      |
|     / \n|    /   \n|   /     \



 ------
|      |
|   \  O  /
|    \ | /
|     \|/
|      |
|      |
|     / \n|    /   \n|   /     \

I am sure there is something simple to be done, but I am just not for sure as I know its having a problem with the \ part before new line "\n" is created.
How to get around this is something I need your help with.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Bob G.
  • 143
  • 3
  • 14

1 Answers1

4

"\\n" makes a literal \n in the printed output.

If you want a \, and a newline, that is "\\\n", where you escape the slash, then put newline.

You could also just println() each line individually. Still need to escape the backslashes, though

    System.out.println();
    System.out.println(" ------ ");
    System.out.println("|      |");
    System.out.println("|      O");
    ...
    System.out.println("|    /   \\");
    System.out.println("|   /     \\");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I know you can just do system.out.println every time, I just thought maybe there is a better way of doing it like the way I was doing it. When I try it with the \\\n it just gives me the invalid sequence. Which was what I was trying to avoid and I put \\ for the legs to solve that problem. So I can only fix it by just doing system.out.println every time? – Bob G. Nov 02 '17 at 02:52
  • It's not a fix. It's to remove the `\n` at the end of the string and guarantee where you have the newlines at. – OneCricketeer Nov 02 '17 at 02:53
  • When I put an \ before the | body part of hangman, is where I get invalid sequence. However, I misinterpreted your comment as a fix. So is there anyway to do it the way I am doing it? Or is that the only way? – Bob G. Nov 02 '17 at 03:23
  • It's a workaround, not a fix. Have you seen the link above? Also, by itself a backslash is an invalid sequence. You always need two characters with any backslash. https://stackoverflow.com/questions/1367322/what-are-all-the-escape-characters#1367339 – OneCricketeer Nov 02 '17 at 03:58
  • I saw the code above but does that work for the arms too? On the stack thread you provided, I tried the b one as only one that made sense to input in and it sort of worked but gave me a box with a ? mark in effectively replacing the / character. – Bob G. Nov 02 '17 at 04:37
  • The forward slashes don't need escaped. Only the left arm and right leg need double backslashes. The right leg needs two slashes plus a newline. I'm confused what you're not understanding – OneCricketeer Nov 02 '17 at 05:00
  • 1
    Ok, that comment helped as its 3 backslashes plus the n character. I thought by newline you meant just n so its total of 2 back slashes. By newline you meant \n. Anyways, I got the last one to work, so I can get the others to work now, just need to do it to the other stages that it is necessary to do so. Thanks for the help. – Bob G. Nov 02 '17 at 14:07