I need to read a text document (with a word per line on it) and extract a word from it to use in a game of hangman. I've been trying to do this for a while, but all I can get is the program to print all of the words in the text file in the console. I think I'm starting to confuse myself. Also, I need to create a class for the player in the game in which they type in their name and it records the number of wins and loses that they have, and for some reason I cannot get my program to do that. Anyway here is the code that I have so far:
import java.util.*;
import java.io.*;
public class hangman
{
public static void main(String [] args) throws IOException
{
int maxTries = 7;
int secretWordLen;
Scanner fileScan = new Scanner(new FileInputStream("words.txt"));
String wordlist = fileScan.next();
String secretWord [] = wordlist[(int)Math.floor(Math.random() * wordlist.length)];
secretWordLen = Math.min(s.length(), maxWordLen);
for (i=0; i<secretWordLen; i++)
{
secretWord[i] = s.charAt(i);
}
System.out.println("Welcome to the game of HANGMAN!!!!");
System.out.println("You will have 7 chances to guess what the word is.");
Scanner inScan = new Scanner(System.in);
System.out.print("Please Enter Your name: ");
String name = inScan.next();
hangPlayer = new player(name);
System.out.println(hangPlayer);
}
}
private class player
{
private player(String name)
{
int winCount = 0;
int loseCount = 0;
System.out.print(name + (", you have won " + winCount + " and you have lost ") + loseCount);
return name;
}
}
THANKS