I do not want to accept same letter to guess and if does , want to show caution and force

to type another try...

and If user got correct letter than I do want to show the letter instead of "_"

_ _ _ _ _ (Example, word is school and if user enter 's' then)

S _ _ _ _


please help me out and please basic coding..

thank you in advance...


-----------------------------------------------------------------------------------------------------

import java.util.*;

public class HangmanTest 
{
      public static void main(String[] args)
      {
            
            String name;
            
            Scanner keyboard = new Scanner(System.in);
            
            Dictionary dic = new Dictionary();
            
            System.out.println("Enter your name to begin....");
            name = keyboard.nextLine();
      
            System.out.println(dic.s);
            System.out.println(dic.t);
            
            System.out.println("The length of word is " + dic.s.length());
                        
            for(int counter = 0; counter < dic.s.length(); counter++)
            {
                  System.out.print("_ ");
            }
            
            System.out.println("");
            System.out.println("");
            
            //System.out.println("Enter the alphabet.");
            //String input = keyboard.nextLine();
            
            //keyboard.nextLine();
            
            
            StringBuffer gLetter = new StringBuffer();
            
            String letter;
            int maxTries = 7;
            while(maxTries > 0)
            {
            System.out.println("The letters that you have guessed are: " + gLetter);
            System.out.print("Please enter a letter to guess: ");
            
            letter = keyboard.next();
            gLetter.append(letter + " ");      
            
            if(dic.s.indexOf(letter) > 1)
            {
                  System.out.println("Please enter one letter at a time");
            
            }
                  else if(dic.s.indexOf(letter) != (-1))
            {
                  
                   dic.s.indexOf(letter);
                   System.out.println("correct");
                   System.out.println(letter);
            }
            else
                  maxTries--;
            System.out.println("You have " + maxTries + " wrong guesses left.");
            }
                  
            
            
            
            
      }
      
      
      

}
import java.util.Scanner;

public class Dictionary 
{
      String words[] = {"dictionary", "technology", "federation", "restaurant", "collection", "enterprise", "television", "communicate", "exaggerate", "responsive"
                            , "automobile", "cappuccino"};
      
      
      String s = words[(int)Math.floor(Math.random() * words.length)];
      
      
      
      
      int t = s.length();
      
      
      
      

      
      Dictionary()
      {
            
      }
      
      

}

Recommended Answers

All 2 Replies

You could store the user's guess in an array of characters which you initialize with '-' characters. When the user gets a letter correct, put that letter in the correct spot in the array. Then when you display the array you will have the dashes and the letters in the right places.

mother PETE

Be a part of the DaniWeb community

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