Skip to content
Advertisement

Text Block and Println Formatting, Java: How to Remove Extra Lines

This is for a hangman game, and the logic works great- the word fills in correctly, and the hangman gets more and more hanged with each word. However, the “graphics” are a little difficult for the user, as you can see below in the output:

3

[ .----------------. 
| .--------------. |
| |  _______     | |
| | |_   __     | |
| |   | |__) |   | |
| |   |  __ /    | |
| |  _| |   _  | |
| | |____| |___| | |
| |              | |
| '--------------' |
 '----------------' ,  .----------------. 
| .--------------. |
| |  ____  ____  | |
| | |_   ||   _| | |
| |   | |__| |   | |
| |   |  __  |   | |
| |  _| |  | |_  | |
| | |____||____| | |
| |              | |
| '--------------' |
 '----------------' , .----------------.s
| .--------------. |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| '--------------' |
 '----------------', .----------------.s
| .--------------. |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| '--------------' |
 '----------------',  .----------------. 
| .--------------. |
| |     ____     | |
| |   .'    `.   | |
| |  /  .--.    | |
| |  | |    | |  | |
| |    `--'  /  | |
| |   `.____.'   | |
| |              | |
| '--------------' |
 '----------------' , .----------------.s
| .--------------. |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| '--------------' |
 '----------------', .----------------.s
| .--------------. |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| '--------------' |
 '----------------',  .----------------. 
| .--------------. |
| |  _______     | |
| | |_   __     | |
| |   | |__) |   | |
| |   |  __ /    | |
| |  _| |   _  | |
| | |____| |___| | |
| |              | |
| '--------------' |
 '----------------' ,  .----------------. 
| .--------------. |
| |     ____     | |
| |   .'    `.   | |
| |  /  .--.    | |
| |  | |    | |  | |
| |    `--'  /  | |
| |   `.____.'   | |
| |              | |
| '--------------' |
 '----------------' , .----------------.s
| .--------------. |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| |              | |
| '--------------' |
 '----------------']

How can it be made flat? The three boxes below show the relevant code for how it was created.

if (word.contains(guess)) {
                                for (int location = 0; location < word.length(); location++) {
                                    String letter = String.valueOf(word.charAt(location));
                                    if (letter.equalsIgnoreCase(guess)) {
                                        progressOnWord.set(location, guess);
                                    }
                                }
                                numberCorrect++;
                                p(numberCorrect);
                        }

                

The above code fills in an ArrayList, progressOnWord, when there is a correct word. progressOnWord starts out as “0000000000”, as many zeros as there are characters in the word up for guessing. It fills in with the right letters if they are guessed, for example:

“rh00o00ro0” at the current stage shown above. This is then converted to ASCII: if the String is 0, add an ASCII block blank to ASCIIform, the ASCII form of progressOnWord. For example, if progressOnWord is rh00o00ro0, then the ASCII block will be what you see above. The blank-setting is indiscriminate-it does not care what location the blank is, it just sets all 0s to blanks.

for (int j = 0; j < progressOnWord.size(); j++) {
                            String wordPlace = progressOnWord.get(j);
                            if (wordPlace.equals("0")) {
                                ASCIIform.set(j, ASCIIblank);
                            }

This statement is discriminate, it makes sure the letter is in the right spot.

for (int k = 0; k < lowercase.length; k++) {
                            String letter = String.valueOf(lowercase[k]);
                            String ASCIIletter = ASCIIalphabet[k];
                            if (wordPlace.equalsIgnoreCase(letter)) {
                                ASCIIform.set(j, ASCIIletter);
                            }
                        }
                        }

Note, the two blocks of code are continuous, but split here for commentary. So back to the question: why are the blocks each on a new line, and how can it be fixed? It’s getting the blocks from an ArrayList<String> (condensed below):

public static String[] getASCIIalphabet() {
        String[] ASCIIalphabet = {
                """
 .----------------.s
| .--------------. |
| |      __      | |
| |     /  \     | |
| |    / /\ \    | |
| |   / ____ \   | |
| | _/ /    \ \_ | |
| ||____|  |____|| |
| |              | |
| '--------------' |
 '----------------'s""",

                """
 .----------------.s
| .--------------. |
| |   ______     | |
| |  |_   _ \    | |
| |    | |_) |   | |
| |    |  __'.   | |
| |   _| |__) |  | |
| |  |_______/   | |
| |              | |
| '--------------' |
 '----------------'s"""
};

Could the formatting of the ArrayList be the problem? Like if you had

"0",
"1",
"2",

in an ArrayList, would it print the following?

0
1
2

I don’t think so. Is it due to the adding of the blocks? They are added 1 by 1 as they are guessed, is that causing the stacking? Or is it some character in the blocks themselves- in the text blocks in the alphabet ArrayList, is it possibly the s?

To provide more detail regarding Daniel’s answer,

StringBuilder.append(char c) and ArrayList.set(int index, String element) are being used.

Here’s the code:

for (int j = 0; j < progressOnWord.size(); j++) {
                                String wordPlace = progressOnWord.get(j);
                                if (wordPlace.equals("0")) {
                                    ASCIIform.set(j, ASCIIblank);
                                }
                                
                            

above, as 0 is the placeholder, when 0 is encountered, it is replaced with an ASCII Block Blank:

                                             '----------------'
                                            | .--------------. |
                                            | |              | |
                                            | |              | |
                                            | |              | |
                                            | |              | |
                                            | |              | |
                                            | |              | |
                                            | |              | |
                                            | '--------------' |
                                             '----------------', 


        
                                for (int k = 0; k < lowercase.length; k++) {
                                    String letter = String.valueOf(lowercase[k]);
                                    String ASCIIletter = ASCIIalphabet[k];
                                    if (wordPlace.equalsIgnoreCase(letter)) {
                                        ASCIIform.set(j, ASCIIletter);
                                    }
                                }
                            


                          

The code above iterates through an alphabet, within the loop that is iterating through each character in the word. for example, with “rhinoceros”: if the progress is rh00o00ro0, it checks if r is 0. No. It then moves on to iterate through a, b, c…z, until it finds r. when it finds r in the alphabet, it notes that r is in spot 0 in the progress, and notes that r is the 18th letter in the alphabet and proceeds to set spot 0 in the ASCII word to the 18th String in the ASCII Block Alphabet:

                                                [ .----------------.
                                                | .--------------. |
                                                | |  _______     | |
                                                | | |_   __     | |
                                                | |   | |__) |   | |
                                                | |   |  __ /    | |
                                                | |  _| |   _  | |
                                                | | |____| |___| | |
                                                | |              | |
                                                | '--------------' |
                                                 '----------------' ,
                             

It is necessary use the ArrayList.set(int index, String element) method because character location matters in this game.

In contrast, StringBuilder.append(char c) is used when created a demo of the ASCII conversion; if the user entered ‘rhinoceros’ it prints the full word ‘rhinoceros’ in the ASCII block style you see above in order to give the user a taste of the “graphics” style. Here’s the code:

                        for (int i = 0; i < allCaps.length(); i++) {
                            char c = allCaps.charAt(i);
                            for (int j = 0; j < uppercase.length; j++) {
                                char bigChar = uppercase[j];
                                String ASCIIblock = getASCIIalphabet()[j];
                                if (c == bigChar) ASCIIword.append(ASCIIblock);
                            }
                        }

It’s worth noting that StringBuilder.append() and ArrayList.set() produce the same result: stacking. Is there a way to append() or set() “sideways”? The append() and set() methods are by default stacking the letters; it seems it must be something with the text blocks formatting in the ArrayList. Although horizontal would normally be the default, can this willy-nilly appending/setting be forced in the horizontal direction, to result in R—->H—->I—->N…?

Advertisement

Answer

I now understand your problem, the problem is that you are appending lines after drawing each character which means you cannot go back to the same line to draw another character so instead you can save everyline in a character in a String array (String[]) in your original ArrayList so it will be ArrayList<String[]> and you can iterate through every first line of a character take look at this code I made to fix the problem:

final int numberOfLines = 11;
for (int i = 0; i < numberOfLines; i++) {
    for (int j = 0; j < asciiForm.size(); j++) {
        String[] word = asciiForm.get(j);
        System.out.print(word[i]);
    }
    System.out.println("t");
}

Example of how a character would look like in the ArrayList:

   asciiForm.add(
                new String[] { 
                        " .----------------. ", 
                        "| .--------------. |", 
                        "| |  _______     | |", 
                        "| | |_   __ \    | |", 
                        "| |   | |__) |   | |", 
                        "| |   |  __ /    | |", 
                        "| |  _| |  \ \_  | |", 
                        "| | |____| |___| | |", 
                        "| |              | |", 
                        "| '--------------' |", 
                        " '----------------' "
                }

        );

The output I got:

OutPut

Note: I did not add all characters to the ArrayList because it was not neccesery for me to do so, I only added R and H

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement