Hi i am in the final stages of my hangman program and need some pointers. I've gotten to a point where i am stuck and need a fresh pair of eyes to help me go over some of my problems any advice given will be greatly appreciated.

import java.util.*;
import java.util.Random.*;
import java.lang.Object;
public class hangman2
{
    static String magic_word;
    static String guessed_so_far = "";

    public static void menu(){ //starts the game asks you if you want to play
        int y;
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 1 for a New Game or 0 to Exit:");
        y = in.nextInt();

    }

    public static void randomize(){ // contains the dictionary of words, randomizes them and WILL PRINT OUT DASHES(TO COME)
        Random r = new Random();
        int randvalue = r.nextInt(19);
        String x;
        String[] possible_values = new String[] {"Happy", "Apple", "Banana", "Cherry", "Plum", "Kiwi", "Peach", "Starfruit", "Carrot", "Tomato", "Potatoe", "Broccoli", "Pepper",
                "Onion", "Necterine", "Clementine", "Grape", "Honeydew", "Peas", "Leeks" };
        magic_word = possible_values[randvalue];
        System.out.println("You Have 6 Incorrect Guesses Left");
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(magic_word); // makes string muteable
        for(int i= 1; i<= magic_word.length(); i++){ //prints out dashes
            System.out.print("_ ");
        }
    }

    public static boolean isinword(String letter) { 
        boolean res = false;

        for(int i = 0; i < magic_word.length(); i++) {
            if (Character.toString(magic_word.charAt(i)) == letter) {
                res = true;
                System.out.println("true");
                break;
            }
        }

        return res;
    }

    public static void guesser(){
        String z;
        int i = 6; //incorrect guesses counter
        Scanner in = new Scanner(System.in);
        System.out.println("Please Enter a letter"); // asks for the user to enter a letter
        z = in.nextLine();
        if(i > 0){  
            if(isinword(z)){ //if z is in the word then it will not subtract the number of incorrect guesses
                if(true){
                    System.out.print(magic_word.charAt(i));
                    System.out.println("You Have "+i+" incorrect guesses left");
                }else if (false){
                    i--;
                    System.out.println("_ ");
                    System.out.println("You Have "+i+" incorrect guesses left.");
                }
            }
            if(i > 0)
                guesser();
        }
    }

    public static void main(String[]args){
        System.out.println("Welcome to Hangman!");
        int y = 1;
        menu();
        if(y == 1){
            randomize();
            guesser();
        }else if( y != 0){
            System.out.println("Please Enter 1  for a new game or 0 to exit");
        }else{
            System.out.println("Goodbye");
        }
    }
}

Recommended Answers

All 5 Replies

This is java, we don't have access to the pointers. :)

What are the problems you need a fresh pair of eyes for?

i can't figure out how to get the letter guessed, if it is in the word to replace the line, and make sure that it picks up everytime it is in the word for example if the user inputs p for the secret word apple i want it to show _ p p _ _

any help you can give is appreciated, this is due at midnight and i feel i'm just missing one small piece

i feel i'm just missing one small piece

That's promising. What is it that it's doing when you run the program? Where exactly are you stuck? You list three components of the problem - can you get the letter?

(use a println to tell you what's in the variable you expect to hold that data)

If you have the letter, are you able to scan the string for instances of that letter?

(use a println to tell you "Found n occurrences of 'p' in String "apple")

When you find that letter, are you able to insert the letter in your output string?

(this should probably be a method: given your current output String, your ciphertext, and your input Character, it returns a new output String - but I don't see any such method in your code, so I don't know what you're doing there)

(Remember: I'm answering this in the time it takes the code I'm writing for pay to build, which at the moment is about 54236milliseconds. Point me right to the problem - I don't have time to take your code apart and figure out where it might be doing something you don't want, and I'm not going to build and test it for you)

i feel my problem lies in here, because i do have the print statement to print out true if the correct answer is there and when i run it, the print statement does not occur.

i greatly appreciate your time and thoughts.

public static boolean isinword(String letter) { 
        boolean res = false;
 
        for(int i = 0; i < magic_word.length(); i++) {
            if (Character.toString(magic_word.charAt(i)) == letter) {
                res = true;
                System.out.println("true");
                break;
            }
        }
 
        return res;
    }

if (Character.toString(magic_word.charAt(i)) == letter) There's your trouble - you don't want to use == to compare Strings, you want .equals() or .equalsIgnoreCase(). See the javadoc for the String class .

This is the same problem that a fellow had just the other day - take a look at this thread for more information on why this is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.