D_Toy
D_Toy

Reputation: 19

Loop and StringBuilder

I am trying to create a guessing game similar to hangman. I have most of the code complete and it compiles fine. However, when I run my code it does not seem to completely run through the program. I am guessing it is something to do with StringBuilder. Any feedback is welcomed. Thank you!

import java.lang.String;
import javax.swing.JOptionPane;
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;

public class GuessGame {
public static void main(String[] args) {
String answer = "G1123 I4 L1123!";
String guessAnswer = "Goose Is Loose!";
StringBuilder sb = new StringBuilder(15);

sb.append("G**** I* L****!");
/*
sb.append("012345678901234");
*/
//create string array and split into
String entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

for(int i = 0; i < 26;i++){
    if (entry.equals("e")){
    sb.replace(4,5,"e");
    sb.replace(13,14,"e");
    JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

    }else if (entry.equals("o")){
    sb.replace(1,3,"oo");
    sb.replace(10,12,"oo");
    JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

    }else if(entry.equals("s")){
    sb.replace(3,4,"s");
    sb.replace(12,13,"s");
    sb.replace(7,8,"s");
    JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

    }else {
    JOptionPane.showMessageDialog(null, "Incorrect letter guess! Try again. "); 
    JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

    }
}


if(answer == guessAnswer){
JOptionPane.showMessageDialog(null, "You win!!! Answer is: " + sb); 
}else{
JOptionPane.showMessageDialog(null, "You Lose: Answer is: " + sb); 
} 
}
}

//edit after Tom's advice:

import java.lang.String;
import javax.swing.JOptionPane;
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;

public class GuessGame {
public static void main(String[] args) {
String answer = "G1123 I4 L1123!";
String guessAnswer = "Goose Is Loose!";
StringBuilder sb = new StringBuilder(15);

sb.append("G**** I* L****!");
/*
sb.append("012345678901234");
*/
//create string array and split into
String entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

for(int i = 0; i < 26;i++){
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);

    if (entry.equals("e")){
    sb.replace(4,5,"e");
    sb.replace(13,14,"e");

    }else if (entry.equals("o")){
    sb.replace(1,3,"oo");
    sb.replace(10,12,"oo");


    }else if(entry.equals("s")){
    sb.replace(3,4,"s");
    sb.replace(12,13,"s");
    sb.replace(7,8,"s");


    }else {
    JOptionPane.showMessageDialog(null, "Incorrect letter guess! Try again. "); 
    JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);


    }
}


if (sb.toString().equals(guessAnswer)){
JOptionPane.showMessageDialog(null, "You win!!! Answer is: " + sb); 
}else{
JOptionPane.showMessageDialog(null, "You Lose: Answer is: " + sb); 
} 
}
}

//Final Code

import java.lang.String;
import javax.swing.JOptionPane;
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;

public class GuessGame {
public static void main(String[] args) {
String answer = "G1123 I4 L1123!";
String guessAnswer = "Goose Is Loose!";
StringBuilder sb = new StringBuilder(15);

sb.append("G**** I* L****!");
/*
sb.append("012345678901234");
*/
//create string array and split into
String entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
 //sorting loop method to find if letter exists

for(int i = 0; i < 26;i++){
entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
    if(sb.toString().equals(guessAnswer)){
    JOptionPane.showMessageDialog(null, "You win!!! Answer is: " + sb); 
    break;

    }else if (entry.equals("e")){
    sb.replace(4,5,"e");
    sb.replace(13,14,"e");

    }else if (entry.equals("o")){
    sb.replace(1,3,"oo");
    sb.replace(10,12,"oo");


    }else if(entry.equals("s")){
    sb.replace(3,4,"s");
    sb.replace(12,13,"s");
    sb.replace(7,8,"s");


    }else{
    JOptionPane.showMessageDialog(null, "Incorrect letter guess! Try again. "); 
    JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);


    }
}
if ((sb.toString().equals(guessAnswer)) == false){
JOptionPane.showMessageDialog(null, "You Lose: Answer is: " + sb);

} 

}
}

Upvotes: 0

Views: 499

Answers (2)

Jonathan Drapeau
Jonathan Drapeau

Reputation: 2610

A few things need to be changed.

First, as noted already, your ending condition should check StringBuilder sb value.

    if (sb.toString().equals(guessAnswer)) {

Also, you need to change what entry contains each time you show the JOptionPane, like this

entry = JOptionPane.showInputDialog(null, "You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);

Now, doing that and clicking on Cancel will throw a NullPointerException, so you need to check if entry is null to stop.

Checking if someone wins should be done more often so you don't wait the 25 turns to get the "Winning" message. Add a isWin(sb) method after someone enters a valid letter would be a good way.

You also forgot, when someone enters e, to change the last e in Loose!.

Upvotes: 1

tom
tom

Reputation: 2714

you never modify the answer variable

replace

if(answer == guessAnswer)

with

if (sb.toString().equals(guessAnswer))

Also, I think it's still not going to work but for other reasons. Put this at the end to find out why:

System.out.println(sb.toString());

Upvotes: 0

Related Questions