Basically, the game goes like so-

User types a word they want player to guess (I've done this)
each letter in the word is displayed by "-"'s (I've done this)
Player guesses a letter in the word (I've done this)
Check guessed letter with each position in the word (I've done this)
Replace "-" for the letter per correct position (Stuck here....)

here's my code:

import java.util.*;

public class Hangman
{
    public static void main(String[]args)
    {
        Scanner kybd = new Scanner(System.in);
        int guesses = 0;
        
        System.out.print("Enter a word to be guessed: ");
        String word = kybd.nextLine();
        
        for (int i = 1; i <= word.length(); i++)
        {
            System.out.print("-");
        }
        System.out.println("");
        System.out.println("");
        
        System.out.print("Enter a letter to be guessed: ");
        String letter = kybd.nextLine();
        char letterchar = letter.charAt(0);
        
        for (int i = 1; i <= word.length(); i++)
        {
            if (letterchar == word.charAt(i))
            {
                //TRIED EVERTYTHING I COULD THINK OF.. ANY IDEAS? T_T
            }
        }
        
    }
}

Thank you

Recommended Answers

All 2 Replies

Use the concept of two arrays in parallel. One is the String of '-' the other is the String of characters for the word.
Given a char input by the user, search the word for a match, if found, replace the '-' at the same position in the String of '-'s.
For example if 'a' matches the second letter in the word, replace the second '-' in the String of '-' with 'a'

Member Avatar for hfx642

I would have done it the hard way, but that's nice and simple NormR1.

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.