-
Hangman Game
The first step in the assignment is to create a Word class with 3 data members. One of type File that gets set by the constructor and two integers called counter and amountOfWords. The class has three methods:
getAmountOfWords() that returns the amount of words
countWords() that counts the how many words are in the File and then sets amountOfWords
nextWord() that returns the next word,making use of counter to keep track of at which word was it before.
I have completed the first two method but I am having trouble with the last one.
This is what I have done so far:
import java.io.*;
import java.util.*;
public class Word
{
File myFile;
int counter=0;
int amountOfWords;
public Word(File f)
{
myFile=f;
}
public int getAmountOfWords()throws FileNotFoundException,IOException
{
countWords();
return amountOfWords;
}
public void countWords()throws FileNotFoundException,IOException
{
int count=0;
FileReader fr=new FileReader(myFile);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line!=null)
{
StringTokenizer st=new StringTokenizer(line);
while(st.hasMoreTokens())
{
String word=st.nextToken();
count++;
}
line=br.readLine();
}
amountOfWords=count;
}
public String nextWord()throws FileNotFoundException,IOException
{
String nextWord="";
ArrayList list = new ArrayList();
FileReader fr=new FileReader(myFile);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line!=null)
{
StringTokenizer st=new StringTokenizer(line);
while(st.hasMoreTokens())
{
list.add(st.nextToken());
}
line=br.readLine();
}
for(int x=0;x<11;x++)
{
}
return nextWord;
}
}
I am not sure how to use the for loop to move through the array list and display the next word using the counter to track the word it was before Ive tried this in the for loop:
counter=list.indexOf(nextWord)+1;
nextWord=list.get(counter).toString();
-
Please put in link to other postings of this problem so people don't waste time repeating solutions.
-
Please could you explain how to do that...
Thanks.
-
It makes more sense to read in all the words only one time and to then access them as needed. Or to read in words until you get to the counter word and return it.
Code:
import java.io.*;
import java.util.*;
public class WordRx
{
File myFile;
int counter=0;
int amountOfWords;
List<String> list;
public WordRx(File f)
{
myFile=f;
// Call to [i]countWords[/i] required for list to be filled.
list = new ArrayList<String>();
}
public int getAmountOfWords() throws FileNotFoundException,
IOException
{
countWords();
return amountOfWords;
}
public void countWords() throws FileNotFoundException,
IOException
{
int count=0;
FileReader fr=new FileReader(myFile);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line!=null)
{
StringTokenizer st=new StringTokenizer(line);
while(st.hasMoreTokens())
{
String word=st.nextToken();
list.add(word);
count++;
}
line=br.readLine();
}
amountOfWords=count;
}
public String nextWord() throws FileNotFoundException,
IOException
{
String nextWord="";
FileReader fr=new FileReader(myFile);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
int count = 0;
while(line!=null)
{
StringTokenizer st=new StringTokenizer(line);
while(st.hasMoreTokens())
{
nextWord = st.nextToken();
if(count == counter) {
counter++;
return nextWord;
}
count++;
}
line=br.readLine();
}
return nextWord;
}
private String getNextWord() {
//if(counter < list.size())
return list.get(counter++);
}
public static void main(String[] args) throws FileNotFoundException,
IOException {
String path = "tutorial/dictionary.txt";
File file = new File(path);
WordRx test = new WordRx(file);
int wordCount = test.getAmountOfWords();
System.out.println("wordCount = " + wordCount);
for(int i = 0; i < 10; i++) {
System.out.println(test.nextWord());
}
for(int i = 0; i < 10; i++) {
System.out.println(test.getNextWord());
}
}
}
-
To add a link, use the icon above the Message input box.
Get the link, the press the icon and you'll get a prompt to enter the link in:
http://aLink.to.nowhere
-
CodeGuru Forums - Programming the Hangman Game
www(dot)codeguru(dot)com/forum/showthread.php?p=1736603#post1736603
Alan
-
Thanks Alan-LB
Thank you Hard Wired the code now reads the next word. I have read the words till I got to the counter and then returned it.
-
I need to update a players Information. I am able to change the players information by the following method:
public String updatePlayerInfo(int playerNr,int correct,int gamesPlayed)throws FileNotFoundException,IOException
{
String playerInfo="";
FileReader fr=new FileReader(myFile);
BufferedReader br=new BufferedReader(fr);
String line=br.readLine();
while(line!=null)
{
StringTokenizer st=new StringTokenizer(line);
String num=st.nextToken();
int playerNum=Integer.parseInt(num);
String name=st.nextToken();
String score=st.nextToken();
int correctGames=Integer.parseInt(score);
String play=st.nextToken();
int played=Integer.parseInt(play);
if(playerNum==playerNr)
{
correctGames=correct;
played=gamesPlayed;
playerInfo=playerInfo+playerNum+" "+name+" "+correctGames+" "+played;
playerInfo=playerInfo+"\n";
}
line=br.readLine();
}
return playerInfo;
}
I am having trouble updating the text file with the new information
-