Reputation: 377
I am writing a hangman program for my assignment and I ran into a small problem. In my program, to separate the words, I am using '|' symbol. Eg This is the word to be guessed:
U N I T E D | S T A T E S | O F | A M E R I C A
I have a StringBuffer variable which displays the no. of dashes Here,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
I tried to use the following code to insert the separator '|'
if(guessword.indexOf('|') == -1)
System.out.print(" ");
else
guessit.setCharAt(guessword.indexOf('|'),'|');
I know its wrong and it inserts only 1 separator. Could you please help me in inserting the separators at the correct positions? Also could you try to remove System.out.print(" "); as it unnecessarily leaves space. Could I use break; instead For the above Eg I get the display as
_ _ _ _ _ _ | _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
I actually want
_ _ _ _ _ _ | _ _ _ _ _ _ | _ _ | _ _ _ _ _ _ _
My main method is as follows
public static void main()throws IOException
{
Hangman obj = new Hangman();
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
PrintWriter p = new PrintWriter(System.out,true);
p.println("Lets play HANGMAN");
p.println();
p.println("Enter your choice according to the following options.\n
NOTE: Words are related to the topics given below.\n
1. Sports\n2. Movies\n3. Computer\n4. Food\n5. Countries");
p.println();
int choice = Integer.parseInt(br.readLine());
obj.clearScreen();
String bothwordandclue[] = new String[2];
if(choice == 1)
bothwordandclue = obj.Sports();
else if(choice == 2)
bothwordandclue = obj.Movies();
else if(choice == 3)
bothwordandclue = obj.Computers();
else if(choice == 4)
bothwordandclue = obj.Food();
else if(choice == 5)
bothwordandclue = obj.Countries();
else
p.println("Wrong choice");
int counter = 6;
String guessword = bothwordandclue[0];
String wordclue = bothwordandclue[1];
int lengthofword = (int)(Math.round(((double)guessword.length()/2)));
int checkguess = 0;
String a;
StringBuffer guessit = new StringBuffer();
for (int i = 0;i < lengthofword; i++)
guessit = guessit.append("_ ");
guessit.delete((2 * lengthofword)-1,(2 * lengthofword));
if(guessword.indexOf('|') == -1)
System.out.print(" ");
else
guessit.setCharAt(guessword.indexOf('|'),'|');
p.println(guessit);
do
{
p.println();
p.println("Enter your guess letter in capitals");
String guessletter = br.readLine();
obj.clearScreen();
for(int i = 0;i<lengthofword;i++)
{
if(guessletter.charAt(0) == guessword.charAt(2*i))
{
guessit.setCharAt(2*i,guessletter.charAt(0));
checkguess=1;
}
}
if(checkguess == 1)
{
p.println("Correct Guess");
p.println(guessit);
}
else
{
counter--;
p.println("Wrong guess. You have " + counter +
" incorrect guesses left");
p.println(guessit);
}
checkguess = 0;
a = guessit.toString();
if(a.equals(guessword))
{
p.println("You guessed the word!!!!!");
counter=0;
}
}while(counter>0);
}
Upvotes: 2
Views: 1242
Reputation: 213261
You can do it with indexOf
only. Just you need to use another version of that method, that takes the position from where to start searching. See String#indexOf(String str, int fromIndex)
that you need here.
int index = guessword.indexOf("|");
while(index >= 0) {
guessIt.setCharAt(index, '|')
// Start searching for next "|" after this index
index = guessword.indexOf("|", index+1));
}
Upvotes: 1
Reputation: 681
The problem is that setCharAt
and indexOf
only apply to one index each, which is why it is only replacing the first |. You will have to iterate through guessword
multiple times. You can either use some of Java's other built-in String functions or use a simple for loop, i.e.
for(int i = 0; i < guessword.length(); i++){
if(guessword.charAt(i) == '|')
guessit.setCharAt(i, '|');
}
Upvotes: 1
Reputation: 96
And if you don't know about regex:
Regular expression on wikipedia
Regular expression cheat sheet
Upvotes: 0