-3

What I'm trying to do is to ask the user to type in a lowercase 5 letter word. Then the next window will have 5 dashes and will ask the user to enter a letter.

If the letter matches one of the letters in the string they input in the beginning, the dash will be replaced with the letter. The computer will ask the user to enter a letter until all the dashes are replaced with the correct letters and every time they input a correct letter, the remaining dashes will appear as well as the correctly guessed words.

for ex: if the word is "hello", a window will appear as " _ _ _ _ _". When I enter an "h" the window will appear as "h _ _ _ _". Then the computer will ask me to input another letter and I type in "l" so the window will show: "h _ l l _".

this is what i have so far:

    String word;
    do{
        word = JOptionPane.showInputDialog("Enter a 5 letter lowercase word");
    }while(word.length()!=5);
    String blanks = "_____";
    char letter = JOptionPane.showInputDialog(blanks).charAt(0);
    for(int a=0;a<=5;a++){
        char blankReplace;
        if(word.charAt(a)==letter){
            blankReplace = letter;
        }
        letter = JOptionPane.showInputDialog(blanks).charAt(0);

Sorry I didnt include this earlier, I'm new

3
  • 4
    ok show us yo code so far? Jan 21, 2014 at 20:29
  • you want us to code for you?
    – Dima
    Jan 21, 2014 at 20:42
  • StringBuilder? Mhm but you still need to do a bit of work to let everything work.. you should have anyway the original string stored somewhere and check it and blablabla. try something you could get the idea. Jan 21, 2014 at 20:44

3 Answers 3

0

how are you storing your 5 characters? it sounds like the easiest way would with with an array. then for your hello example you can go from:

array[0] = '_';
array[1] = '_';
array[2] = '_';
array[3] = '_';
array[4] = '_';

to:

array[0] = 'h';
array[1] = '_';
array[2] = '_';
array[3] = '_';
array[4] = '_';
0

You could store the word in one array and use another array as the masked word. Then fetch user input and loop through the arrays to replace letters when found. Like so:

char word[] = "qwert".toCharArray();
char mask[] = "_____".toCharArray();    
char input = getUserInput();    //made up method, of course...

for(int i = 0; i < word.length; i++) {
    if(word[i] == input) {
        mask[i] = input;
    }
}
//repeat until solved, use a counter or something...
0

Uhm, ok.

This is a sample code which should do what you want. It takes a String (from user input or you insert the string, edit the code) Then create a StringBuilder which will contains the new string with "_" It works with any string (or should, i have test some cases).

package com.abacus.carnevalevenezia;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{
    public static void main(String[] args)
    {
        String originalWord = "Hello You".toLowerCase();
        StringBuilder currentWork = new StringBuilder(originalWord.length());

        for (int i = 0; i < originalWord.length(); ++i)
            if (originalWord.charAt(i) != ' ')
                currentWork.append("_");
            else
                currentWork.append(" ");

        System.out.println("Word: " + currentWork.toString());

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        do
        {
            try
            {
                char temp = Character.toLowerCase(bufferedReader.readLine().charAt(0));

                // Search for all positions
                int idx = 0;
                int lastIDX = 0;

                while ((idx = originalWord.indexOf(temp, lastIDX)) != -1)
                {
                    currentWork.replace(idx, idx + 1, Character.toString(temp));

                    lastIDX = idx + 1;
                }

                if (originalWord.equals(currentWork.toString()))
                {
                    System.out.println("You win (" + originalWord + ").");
                    break;
                }
                else
                {
                    System.out.println("Word: " + currentWork.toString());
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } while (true);
    }
}

You can use char arrays too. But it's just a sample code which do the basic work. I convert everything to lower case but if you want to keep the original case edit it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.