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.
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.
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.
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.
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?
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'
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.
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.
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).