Hello. I'm just beginning to learn Java and right now I'm working on simple text-based Hangman. But I am having problem replacing unknown letter with correctly guessed letter.
(i.e "_ _ _ _" unknown word is roll. User inputs "l", "_ _ _ _" this becomes "_ _ l l")
I looked at several hangman threads here but I still couldn't understand how I could replace characters.
This is what I have so far :
public static void main(String[] args) throws FileNotFoundException {
int tries = 7;
Scanner scan = new Scanner(System.in);
System.out.println("=Hangman=\n");
Hangman.random();
System.out.println("\n\nGuess a letter:");
String guess = scan.nextLine();
public static void random() throws FileNotFoundException {
Random gen = new Random();
int ran = gen.nextInt(113810);
String wordArr[];
wordArr = getWords();
String ranWord = wordArr[ran];
System.out.println(ranWord);
for (int i = 0; i < ranWord.length(); i++) {
System.out.print("_ ");
}
}
public static String[] getWords() throws FileNotFoundException {
Scanner fileScan = new Scanner(new File("words.txt"));
String words[] = new String[113809];
String dummy = "";
for (int i = 0; i < 113809; i++) {
dummy = fileScan.nextLine();
words[i] = dummy;
}
return words;
}
Can anyone hint me how I could replace " _ " < these with correct letter?