Letters not being correctly inserted into a StringBuilder

Building a Hangman game, and I have a problem with printing the letters the user has already guessed correctly. What happens is that the first letter the user guesses correctly always goes in no problem. Ever correct letter after the first letter sometimes goes in and sometimes doesn't, and I can't figure out a system. It might have something to do with lag, which is possible because my computer isn't great.

Here are some variable declarations you might want.

StringBuilder orderedLetters = new StringBuilder("_____");
JLabel label = new JLabel(orderedLetters);
String word = "horse";//word is always a randomly generated 5 letter word

Here is the method where I believe the trouble is.

    void printLetterSpaces() {
    //inserts the first letter of the word and deletes the one it moved out of the way
    if      (isLetterSolved(0, 1)) {
        orderedLetters.insert(0, word.substring(0,1));
    //inserts the second letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(1, 2)) {
        orderedLetters.insert(1, word.substring(1,2));
    //inserts the third letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(2, 3)) {
        orderedLetters.insert(2, word.substring(2,3));
    //inserts the fourth letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(3, 4)) {
        orderedLetters.insert(3, word.substring(3,4));
    //inserts the fifth letter of the word and deletes the one it moved out of the way
    else if (isLetterSolved(4, 5)) {
        orderedLetters.insert(4, word.substring(4,5));
    System.out.println(userInput.getText().substring(0, 1));

Here is my method referenced in the above method:

boolean isLetterSolved(int x, int y) {
        return true;
        return false;

Before guessing a letter: https://gyazo.com/d6d547250e0da4cc0319e1f7e5b2fc14

After correctly guessing a letter for the first time(O is the letter i guessed): https://gyazo.com/f2d8535f8a46629ba9fa9d336faa7c2e

After correctly guessing a letter the second time (r is the letter i guessed(it made the jlabel turn to correct guess)): https://gyazo.com/159e7c82c3f07fc5b18df51fbf00bbef

The method that tests if the letter is correct is completely correct, because the JLabel wouldn't turn tell you your guess is correct without calling printLetterSpaces

EDIT: Well, I solved it and im not sure why it works but here is the solved method:

void printLetterSpaces() {
    //inserts the first letter of the word and moves the one it replaced out of the way
    if      (isLetterSolved(0, 1)) {
        orderedLetters.replace(0, 1, word.substring(0, 1));
    //inserts the second letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(1, 2)) {
        orderedLetters.replace(1, 2, word.substring(1, 2));
    //inserts the third letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(2, 3)) {
        orderedLetters.replace(2, 3, word.substring(2, 3));
    //inserts the fourth letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(3, 4)) {
        orderedLetters.replace(3, 4, word.substring(3, 4));
    //inserts the fifth letter of the word and moves the one it replaced out of the way
    if (isLetterSolved(4, 5)) {
        orderedLetters.replace(4, 5, word.substring(4, 5));
    System.out.println(userInput.getText().substring(0, 1));


Not exactly the same way but below code solve the problem using different approach.

Create a DTO.

public class WordDTO {

    private String orignalWord;

    private String newWord;

    private String guessdWord;

    private char inputChar;

    private int position;

    private boolean status;

    public String getNewWord() {
        return newWord;

    public void setNewWord(String newWord) {
        this.newWord = newWord;

    public String getGuessdWord() {
        return guessdWord;

    public void setGuessdWord(String guessdWord) {
        this.guessdWord = guessdWord;

    public char getInputChar() {
        return inputChar;

    public void setInputChar(char inputChar) {
        this.inputChar = inputChar;

    public int getPosition() {
        return position;

    public void setPosition(int position) {
        this.position = position;

    public boolean isStatus() {
        return status;

    public void setStatus(boolean status) {
        this.status = status;

    public String getOrignalWord() {
        return orignalWord;

    public void setOrignalWord(String orignalWord) {
        this.orignalWord = orignalWord;


The class which take input from user and generates the random word and then ask the user to guess the word. I have initialized the initial guessed word with 012345 i.e from 0 to size of word. You can initialize with "_"

public class GuessTheWordMain {

    public static void main(String args[]) {
        Scanner reader=new Scanner(System.in);
        System.out.println("Enter the size of Word to guess!!!!");
        int sizeOfNum=reader.nextInt();
        String word=generateRndomWord(sizeOfNum);
        WordDTO wordDTO=new WordDTO();
        String guesWordInitial="";
        for (int j=0;j<word.length();j++) {
        for(int i=0;i<sizeOfNum;i++) {
            System.out.println("Enter the Guess Number :"+i);
            char guess=reader.next().charAt(0);



    public static String generateRndomWord(int size) {
        Random random=new Random();
        StringBuilder stringBuilder=new StringBuilder(size);
        for(int i=0;i<size;i++) {
        return stringBuilder.toString();

    public static WordDTO displayGuessResult(WordDTO wordDTO) {
        String newWord=wordDTO.getNewWord();
        boolean status=wordDTO.isStatus();
        int position=-1;
        for (int i=0;i<newWord.length();i++) {

            if((newWord.charAt(i))==wordDTO.getInputChar()) {
        if(status) {
            wordDTO.setNewWord(newWord.replace(newWord.charAt(position), '*'));
            wordDTO.setGuessdWord(wordDTO.getGuessdWord().replace(wordDTO.getGuessdWord().charAt(position), wordDTO.getInputChar()));
        return wordDTO;


Please ask in case of any doubt/improvement.

Need Your Help

referencing method in the inner class from the outer class

java class inner-classes

i have the below code, and despite the classes and the memeber methods are public i couldnt reference methodHF not methodBF inside the methodLMF. i tried the following: