Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-10-2012, 04:18 PM   PM User | #1
MoodySpark
New to the CF scene

 
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
MoodySpark is an unknown quantity at this point
Need help with Java BlueJ hangman game

Hey all,
I am trying to write a program in BlueJ where the user has to input the words.txt file themselves and I am stuck because I don't know which code to use.
Would it be a BufferedReader you need or what?
Any help would be appreciated thanks.
MoodySpark is offline   Reply With Quote
Old 08-10-2012, 05:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Depends on exactly what is in that file.
I'd use a BufferedReader if it was a line by line based text that I need to parse and process. If its serialized, then you would use something like an Object stream.
Fou-Lu is offline   Reply With Quote
Old 08-11-2012, 10:33 AM   PM User | #3
MoodySpark
New to the CF scene

 
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
MoodySpark is an unknown quantity at this point
well its a txt file with a list of about five words so the user can input that into the program themselves, but I suppose it's a hangman game so somehow I will have to code it so it shuffles the words and picks one at random.
MoodySpark is offline   Reply With Quote
Old 08-11-2012, 04:19 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Depends on how fancy and efficient you want to be.
For a few words, the easiest way is to simply read all the lines into a collection such as an ArrayList<String> or Vector<String> using the BufferedReader's readLine method, and then execute a Collections.suffle on your list. Simply pull the first item out when complete.

For something more efficient if you were using hundreds or thousands of words, I'd use a random access file. It wastes more space overall since you space pad strings that are less than your maximum size, but you can jump right to the record in the file to read it, effectively giving you an O(1) access magnitude. I consider O(1) on a lookup > the waste of disk space. This works by allowing the file to contain two numbers at the head of the file, one representing the maximum length of the string, and another representing the number of records (using a byte and a long would work for this example). Then you use the seek method to move to the start of the record (record * size of record + 33 bytes), and read the next size of record. The random can occur between 1 and the number of records.
Fou-Lu is offline   Reply With Quote
Old 08-13-2012, 09:30 PM   PM User | #5
MoodySpark
New to the CF scene

 
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
MoodySpark is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
Depends on how fancy and efficient you want to be.
For a few words, the easiest way is to simply read all the lines into a collection such as an ArrayList<String> or Vector<String> using the BufferedReader's readLine method, and then execute a Collections.suffle on your list. Simply pull the first item out when complete.

For something more efficient if you were using hundreds or thousands of words, I'd use a random access file. It wastes more space overall since you space pad strings that are less than your maximum size, but you can jump right to the record in the file to read it, effectively giving you an O(1) access magnitude. I consider O(1) on a lookup > the waste of disk space. This works by allowing the file to contain two numbers at the head of the file, one representing the maximum length of the string, and another representing the number of records (using a byte and a long would work for this example). Then you use the seek method to move to the start of the record (record * size of record + 33 bytes), and read the next size of record. The random can occur between 1 and the number of records.
Okay, well i've done something like this for the array list:

try {

BufferedReader br= new BufferedReader(new FileReader("words.txt));
List<String> lines = new ArrayList<String>();
String line = br.readLine();
while(line !=null){
lines.add(line.replace(">", ""));
line = br.readLine();
}

not sure if it's right but if it is, how would I get a shuffle from after that?
MoodySpark is offline   Reply With Quote
Old 08-13-2012, 09:42 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You would call Collections.shuffle(lines); after the loop.
Looks good otherwise (assuming you have a catch for the io exceptions).
Fou-Lu is offline   Reply With Quote
Old 08-13-2012, 10:29 PM   PM User | #7
MoodySpark
New to the CF scene

 
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
MoodySpark is an unknown quantity at this point
well i've put throws IO Exception at the public static void main thingy, erm but the try comes with an error in blue J it says: 'try' without 'catch' or 'finally'

whatever that means
MoodySpark is offline   Reply With Quote
Old 08-13-2012, 10:33 PM   PM User | #8
MoodySpark
New to the CF scene

 
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
MoodySpark is an unknown quantity at this point
this is my full code so far just in case:

import java.util.*;
import java.util.Scanner;
import java.io.*;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class Hangman
{


public static void main (String[] args) throws Exception {


FileReader fr = new FileReader(word.txt);

try {
BufferedReader br= new BufferedReader(new FileReader("words.txt"));
List<String> lines = new ArrayList<String>();
String line = br.readLine();
while(line !=null){
lines.add(line.replace(">", ""));
line = br.readLine();
}
Collections.shuffle(lines);
}


}
MoodySpark is offline   Reply With Quote
Old 08-13-2012, 10:36 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
You cannot use a try without a catch/finally block. You can remove the try completely since you've thrown Exception, but a better solution would be to try/catch and handle it gracefully then allowing the main to throw an exception (which terminates the program).
Don't forget to actually do something with lines after. Right now, you simply collect them and shuffle it, but you don't have any output or any other processing.
Fou-Lu is offline   Reply With Quote
Old 08-13-2012, 10:58 PM   PM User | #10
MoodySpark
New to the CF scene

 
Join Date: Aug 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
MoodySpark is an unknown quantity at this point
right well I removed the try so it's just collecting the txt file and putting it into an Array list, I suppose I just need it to start giving the user a number of guesses to the word now.
MoodySpark is offline   Reply With Quote
Old 08-14-2012, 04:40 AM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by MoodySpark View Post
right well I removed the try so it's just collecting the txt file and putting it into an Array list, I suppose I just need it to start giving the user a number of guesses to the word now.
Yep, but you need to choose a word first. You can try/catch a lines.get(0) to pull the first word out of the list. Its not actually a checked exception, but just like the read you should try/catch gracefully (readline is actually a checked exception which is why you must try/catch or throws off of the method using it otherwise the compiler throws an error).
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:46 AM.


Advertisement
Log in to turn off these ads.