Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-02-2008, 01: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();
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-02-2008, 02:54 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 972
Norm is on a distinguished road
Please put in link to other postings of this problem so people don't waste time repeating solutions.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-02-2008, 03:12 PM
L23 L23 is offline
Member
 
Join Date: Jul 2008
Posts: 5
L23 is on a distinguished road
Please could you explain how to do that...

Thanks.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-02-2008, 10:45 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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 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()); } } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-03-2008, 12:04 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 972
Norm is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-03-2008, 05:10 AM
Alan-LB's Avatar
Member
 
Join Date: Jun 2008
Location: Junee, NSW, Australia
Posts: 19
Alan-LB is on a distinguished road
Send a message via Yahoo to Alan-LB
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!!
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-03-2008, 10:04 AM
L23 L23 is offline
Member
 
Join Date: Jul 2008
Posts: 5
L23 is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-03-2008, 01:28 PM
L23 L23 is offline
Member
 
Join Date: Jul 2008
Posts: 5
L23 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-03-2008, 02:56 PM
L23 L23 is offline
Member
 
Join Date: Jul 2008
Posts: 5
L23 is on a distinguished road
I have solved the above.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
game amith AWT / Swing 0 05-19-2008 06:16 PM
Battery 1.00 (Game) M77 Java Announcements 1 05-12-2008 10:56 AM
Implementing "Game Over" in Minesweeper game based on Gridworld framework. JFlash New To Java 0 11-16-2007 12:02 AM
Create the game Hangman barney New To Java 1 08-06-2007 07:16 AM
Help with pong game Eric New To Java 2 07-03-2007 08:02 PM


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