|
|
|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

07-02-2008, 01:34 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 5
|
|
|
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();
|
|

07-02-2008, 02:54 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 972
|
|
|
Please put in link to other postings of this problem so people don't waste time repeating solutions.
|
|

07-02-2008, 03:12 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 5
|
|
|
Please could you explain how to do that...
Thanks.
|
|

07-02-2008, 10:45 PM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,146
|
|
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.
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 countWords 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());
}
}
}
|
|

07-03-2008, 12:04 AM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: SW MO, USA
Posts: 972
|
|
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
|
|

07-03-2008, 05:10 AM
|
 |
Member
|
|
Join Date: Jun 2008
Location: Junee, NSW, Australia
Posts: 19
|
|
|
CodeGuru Forums - Programming the Hangman Game
www(dot)codeguru(dot)com/forum/showthread.php?p=1736603#post1736603
Alan
__________________
There are 10 types of people - those who understand binary and those who don't!!
Today is the Beta version of Tomorrow!!
|
|

07-03-2008, 10:04 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 5
|
|
|
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.
|
|

07-03-2008, 01:28 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 5
|
|
|
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
|
|

07-03-2008, 02:56 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 5
|
|
|
I have solved the above.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
All times are GMT +3. The time now is 06:14 PM.
|
|
VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org