Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    You would call Collections.shuffle(lines); after the loop.
    Looks good otherwise (assuming you have a catch for the io exceptions).

  • #7
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #8
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);
    }


    }

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

  • #10
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •