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;
}
}