0

i am building a word puzzle game in java that retrieves words from an array and hides some of the characters for you to guess. The words are retried from an array, but the answers are entered using scanner.The program works. what i am trying to do is make the loop display only the current item in the array index without displaying the previous ones in the list.

Here is an example:
I don't want this!!

word:gove***ment
answer: government

word: po***e
answer: police

word:qu***ty
answer: quality

...the loop terminates displaying all the items in the loop, just like a normal loop

here is what i want!
word: po***e
answer: police

...the loop displays only the current item, while expecting the next item to be entered

Here's my entire code: import java.util.Scanner; public class WordPuzzleTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String [] arr= {"government",
                        "senate",
                        "president",
                        "minister",
                        "senator",
                        "parliament",
                        "legislature",
                        "judiciary",
                        "advisers",
                        "governor"};
        int index=0;
        String userInput;
        String newWord;
        int score=0;
        //double performance;
        int length=arr.length;

        WordPuzzle getInput=new WordPuzzle();

        System.out.println("--INSTRUCTION--");
            System.out.println("A Word will be given, you have to type the complete word");
            System.out.println("eg: ban***a");
            System.out.println("answer: bannana");
            System.out.println("--NOW BEGIN--");
            System.out.println("");

        while(index<arr.length){


            System.out.println("what is this word");

            //get the word from the array
            newWord=concealWord(arr[index]);
            System.out.println(newWord);
            userInput=getUserInput();

                if(userInput.equals(arr[index])){
                     System.out.println("correct!!!");
                     score++;
                }
                else{
                    System.out.println("Wrong!!!");
                }

                index++;
        }
        //calculate perforamance
         String star;
         if(score<=length/2){
              star="**2 stars";
            }
         else{
              star="***3 stars";
         }
          System.out.println("");
          System.out.println("--YOUR FINAL SCORE--");
          System.out.printf("Your score %d...\n(%s)",score,star);

    }
   // the getUserInput and concealWord methods

    public String getUserInput(){
    String answer;
    Scanner input= new Scanner(System.in);
    answer=input.next();
    return answer;
}

public String concealWord(String word){
    int wrdlength=word.length();

    int beginIndex=wrdlength/2-1;
    int endIndex=beginIndex+3;

    // get the substrings of the characters we want to replace
    String oldChar=word.substring(beginIndex, endIndex);
    String newChar="***";

    //replace the old characters with new ones
    String replacement=word.replace(oldChar, newChar);

    return replacement;

}

}
Alex Irabor
  • 401
  • 1
  • 6
  • 17
  • Not quite sure what you mean, do you mean u want to input e.g. po**ce and program returns you "police"? – nafas Aug 05 '14 at 13:04
  • Do you want to clear the console for each iteration? – naveejr Aug 05 '14 at 13:06
  • 1
    If you want to clear the console Check this out http://stackoverflow.com/questions/3095986/how-to-get-backspace-b-to-work-in-eclipses-console – naveejr Aug 05 '14 at 13:16
  • What does method `getUserInput()` do ? – vikingsteve Aug 05 '14 at 13:21
  • The getUserInput() gets input from the user using scanner, the getUserInput() works just fine. i just need the loop to display only the current item in the array and when the next item is displayed the previous item should no longer visible in the loop. i have re-edited the code to contain the getUserInput() and concealWord() methods – Alex Irabor Aug 06 '14 at 03:01

0 Answers0