Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am still having problems with my output. I get a message that I guess the wron

ID: 3620357 • Letter: I

Question

I am still having problems with my output. I get a message that I guess the wrong letter at the same time that i get the message that I guessed the correct letter.

import java.util.*;
import java.io.*;


public class Hangman
{
public static void main(String[] args) throws IOException
{
int words,n,i,choice;
Random gen = new Random();
int maxTries = 5;
int wordLength,index,foundletters;
boolean solved,found;
Scanner inScan = new Scanner(System.in);
char letter;
String secretWord="";


//the fileScan gets the first word for the game

System.out.println("Welcome to Hangman!");
System.out.println("You can");
System.out.println(" 1. Start a new game.");
System.out.println(" 2. Quit.");
System.out.println("Please enter your choice: ");
choice = inScan.nextInt();

while(true)
{switch (choice)
{
//default: System.out.println("Please 1 for a new game, or 2 to exit . . ."); break;
case 1:{
StringBuffer guessedLetters = new StringBuffer();
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
words=fileScan.nextInt();
maxTries = 5;
n=gen.nextInt(words);
for(i=0;i<=n;i++)
secretWord = fileScan.next().toLowerCase();
fileScan.close();
foundletters=secretWord.length();
//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(i = 0; i < secretWord.length(); i++)
word.append("_ ");
//System.out.println(secretWord);

//System.out.println("Your word is " + secretWord.length() + " letters long.");
System.out.println("Here's the board: ");
System.out.println(word);
solved=false;
while(maxTries > 0&&!solved)
{
found=false;
//System.out.println("The letters that you have guessed are: " + guessedLetters);
System.out.print("Please enter your guess: ");
//Scanner inScan = new Scanner("words.txt");
letter = Character.toLowerCase(inScan.next().charAt(0));
guessedLetters.append(letter + " ");
index=0;
while(secretWord.indexOf(letter,index) != (-1))
{
found=true;
word.setCharAt(secretWord.indexOf(letter,index)*2,letter);
foundletters--;
index=secretWord.indexOf(letter,index)+1;
if(foundletters==0)
{solved=true;
System.out.println("Congratulations, you win!");
System.out.println("Here is the final board:");
System.out.println(word);
}
if(!solved)
{System.out.println("Great, You made a correct Guess!");
System.out.println("Let's keep going.");
System.out.println("_________________________________________");
System.out.println("The word: "+word);
}
}
if(!found)
maxTries--;
if(!solved)
System.out.println("Oops, you made an incorrect guess, the letter " + letter + " is not a letter of the secret word");
System.out.println("_________________________________________");
}
}
if(!solved)
{System.out.println("Sorry, you have not guessed the correct word in time.");
System.out.println("the correct word is "+secretWord);
System.out.println("You lose. ");
}
break;
case 2:System.exit(0);

default: System.out.println("Please enter 1 for a new game, or 2 to exit . . .");break;
}

System.out.println(" 1. Start a new game.");
System.out.println(" 2. Quit.");
System.out.println("Please enter your choice: ");
choice = inScan.nextInt();



}
}
}

Explanation / Answer

please rate - thanks 1 simple correction see the red import java.util.*;
import java.io.*;


public class Hangman
{
public static void main(String[] args) throws IOException
{
int words,n,i,choice;
Random gen = new Random();
int maxTries = 5;
int wordLength,index,foundletters;
boolean solved,found;
Scanner inScan = new Scanner(System.in);
char letter;
String secretWord="";


//the fileScan gets the first word for the game

System.out.println("Welcome to Hangman!");
System.out.println("You can");
System.out.println(" 1. Start a new game.");
System.out.println(" 2. Quit.");
System.out.println("Please enter your choice: ");
choice = inScan.nextInt();

while(true)
{switch (choice)
{
//default: System.out.println("Please 1 for a new game, or 2 to exit . . ."); break;
case 1:{
StringBuffer guessedLetters = new StringBuffer();
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
words=fileScan.nextInt();
maxTries = 5;
n=gen.nextInt(words);
for(i=0;i<=n;i++)
   secretWord = fileScan.next().toLowerCase();
fileScan.close();  
foundletters=secretWord.length();
//Creates a StringBuffer for the viewing of the letters guessed
StringBuffer word = new StringBuffer();
for(i = 0; i < secretWord.length(); i++)
word.append("_ ");
//System.out.println(secretWord);

//System.out.println("Your word is " + secretWord.length() + " letters long.");
System.out.println("Here's the board: ");
System.out.println(word);
solved=false;
while(maxTries > 0&&!solved)
{
found=false;
//System.out.println("The letters that you have guessed are: " + guessedLetters);
System.out.print("Please enter your guess: ");
//Scanner inScan = new Scanner("words.txt");
letter = Character.toLowerCase(inScan.next().charAt(0));
guessedLetters.append(letter + " ");
index=0;
while(secretWord.indexOf(letter,index) != (-1))
{
found=true;
word.setCharAt(secretWord.indexOf(letter,index)*2,letter);
foundletters--;
index=secretWord.indexOf(letter,index)+1;
if(foundletters==0)
     {solved=true;
      System.out.println("Congratulations, you win!");
      System.out.println("Here is the final board:");
     System.out.println(word);
      }
if(!solved)
   {System.out.println("Great, You made a correct Guess!");
    System.out.println("Let's keep going.");
    System.out.println("_________________________________________");
    System.out.println("The word: "+word);
    }
}
if(!found)
     maxTries--;
      if(!solved&&!found)
         System.out.println("Oops, you made an incorrect guess, the letter " + letter + " is not a letter of the secret word");
System.out.println("_________________________________________");
}
}
if(!solved)
    {System.out.println("Sorry, you have not guessed the correct word in time.");
      System.out.println("the correct word is "+secretWord);
      System.out.println("You lose. ");
      }
break;
case 2:System.exit(0);
     
default: System.out.println("Please enter 1 for a new game, or 2 to exit . . .");break;
}

System.out.println(" 1. Start a new game.");
System.out.println(" 2. Quit.");
System.out.println("Please enter your choice: ");
choice = inScan.nextInt();



}
}
}
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote