Thread: Hangman Game
View Single Post
  #1 (permalink)  
Old 07-02-2008, 02:34 PM
L23 L23 is offline
Member
 
Join Date: Jul 2008
Posts: 5
L23 is on a distinguished road
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();
Reply With Quote
Sponsored Links