Hi, I'm new here. I have been looking around for the last few months. Very new to java and I need help with this assignment. I am to program hangman, I know that this has been asked multiple times. I just cannot seem to find the answers that I need. This is what I have so far. I can't seem to figure out what I need to do to print the letters in the correct underscore. I was thinking the block comment section can be used to search for used letters. I think that is all that I have to do in my assignment, and it's the stuff that I don't understand or cannot think of a way to do it. Any help would be greatly appreciated.
Thanks in advance
import java.util.*;
import java.io.*;
public class Assig3
{
public static void main(String [] args) throws IOException
{
boolean winYes;
int wins;
int i;
int guesses = 7;
char [] letters = new char [26];
int index;
char lettersGuessed;
int Wordquit = 2;
int missWord = 0;
Scanner inScan = new Scanner(System.in);
System.out.println("Please enter your name\n");
Name.getName(inScan.next());
//Loads the file as specifed by user
wordServer theWords = new wordServer();
System.out.println("Enter file name: ");
Scanner keyScan = new Scanner(System.in);
String fName = keyScan.next();
File inFile = new File(fName);
Scanner fScan = new Scanner(inFile);
theWords.loadWords(fScan);
// creates current word object
String current;
current = theWords.getNextWord();
StringBuilder guessedWord = new StringBuilder();
//Shows user underscores of letters needed.
do
{
System.out.print("Your word is: ");
i = 0;
while( i < current.length())
{
i++;
guessedWord.append('_');
}
System.out.println(guessedWord);
index = 0;
while(guesses > 0)
{
System.out.println("You have " + guesses + " left."); // tells user number of guesses left
}
for(i = 0; i <= index - 1; i++)
{
System.out.println(letters[i]);
}
//prompts user to make a guess.
System.out.println("Please make a guess: ");
lettersGuessed = inScan.nextChar();
/*
char guess = letter.charAt(0);
boolean correctGuess = false;
for(int k = 0; k < current.length(); k++)
{
if(guess == digit[i])
{
hidden[i] = guess;
correctGuess = true;
}
}
*/
letters[index] = lettersGuessed;
index++;
System.out.println("Enter 0 to quit word: ");
System.out.println("Enter 1 to quit word: ");
wordQuit = inScan.nextInt();
while(wordQuit = 2);
}
}
import java.util.*;
import java.io.*;
public class wordServer
{
String [] words;
int num;
int [] usedInd;
int i;
String empty = " ";
public wordServer()
{
i = 0;
}
public void loadWords(Scanner S) throws IOException // loads "dictionary" of words.
{
num = S.nextInt();
words = new String [num];
usedInd = new int [num];
for (int i = 0; i < num; i++)
words[i] = S.nextLine();
S.close();
}
public String getNextWord() // Finds a random index number. then returns a random word.
{
do
{
int ranInd = (int)(words.length * Math.Random()) + 1
int hit = 0;
for(int j = 0; j <= i - 1; j++)
{
if(ranInd == usedInd[j])
{
hit = 1;
}
}
while(hit = 1);
usedInd[i] = ranInd;
i++;
return words[ranInd]
}
}
}
I also have one last class which is hangPlayer. All this has is holding information of the player. name, wins, misses. And then a StringBuilder which returns the information in a neat and organized way.