A few days ago, i made a hangman game and I really want to complete it in any aspect. One of my ideas now is to "import" words from a file. Currently what I have is an Array of Strings (just a few words inside for testing) and what I want is a file containing those 2:
Word Hint
word1 Hint1 with spaces
word2 Hint2 with spaces

What I am wondering now is can I import the file and all its context into an array maybe? and then when the game starts use regular expressions such as randomly take a row from the size of the file , then write to the GUI as the word to guess the contense of the row up to the first empty space and then after the space write the contense as Hint area in the GUI. I've had some experience with regular expressions, would they be able to give me the word/hint ? Or any other ways would be appreciated. Also, if this won't work what about 1 file hints, 1 file words, import into ArrayLists and then just get the index of a word and use the same index to get the hint from the other arraylist?

Recommended Answers

All 10 Replies

Separating words and hints is a forumla for update chaos.
Just read the file 1 line at a time. Get the index of the first " " and use that to substring the word and the rest of the line (the hint). Store them in 2 ArrayLists (or create a class to hold them both if you want to be clever). They won't occupy much storage.

Then use random ints to index into the arraylists to get a word and hint.

No need for REGEXs

Second thoughts:
Previous post does too much messing around from the List too early. Here's a better suggestion:
Just use Files.readAllLines(Path p) to get all the data into a List, then use random ints to pick lines, and then split the chosen lines into word/remainder.

Thanks :)

Hey James ... I am trying to make any communication at all to my file, currently this is what I have

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFile {

    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new FileReader("/home/words.txt")))
        {

            String sCurrentLine;

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } 

    }
}

If I run it on my windows laptop it works fine, but it doesn't seem to work on linux, I think it is because of the path? This is the Error list

ReadFile.java:9: '{' expected
        try (BufferedReader br = new BufferedReader(new FileReader("/home/words.txt")))
           ^
ReadFile.java:9: ')' expected
        try (BufferedReader br = new BufferedReader(new FileReader("/home/words.txt")))
                           ^
ReadFile.java:9: ';' expected
        try (BufferedReader br = new BufferedReader(new FileReader("/home/words.txt")))
                                                                                      ^
ReadFile.java:18: 'catch' without 'try'
        } catch (IOException e) {
          ^
ReadFile.java:18: ')' expected
        } catch (IOException e) {
                            ^
ReadFile.java:18: not a statement
        } catch (IOException e) {
                ^
ReadFile.java:18: ';' expected
        } catch (IOException e) {
                              ^
ReadFile.java:9: 'try' without 'catch' or 'finally'
        try (BufferedReader br = new BufferedReader(new FileReader("/home/words.txt")))
        ^
ReadFile.java:23: reached end of file while parsing
}
 ^
9 errors

EDIT: Using pwd found that the path was /home/slavi/textfile but still didnt work

Hmm .. I tried using scanner and it magically works :D

File file = new File("/home/slavi/words.txt");

        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line);
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

Maybe your Linux Java is 1.6 and your Windows Java is 1.7 or 1.8? (The try-with-resources construct was new in Java 7. You use that in the first version, but not in the Scanner version.)
If so, time to update! 1.8 is the current version on Windows and Linux

Mhm, i had them both up to date ;) is scanner efficient to use for this purpose? I dont see more than 1000 lines in total, from what I found on google scanner is decent for small/middle sized files but noone really pointed out what is middle sized files, I assume with 1000 it should work? Or should I just use windows to finish the program and use BufferedReader?

BufferedReader is the simpler and more direct way to read lines from text file, but the overheads of scanner for readling lines will be tiny.
Either will be just fine on files of many megabytes.

I don't know why there is tht Linux problem, but if Scanner works for you in both environments then that's what you should use!

Hey James, morning

I have the following code that can now read a file, return the number of lines and also return the contense of a line.

public class ReadFile {
    private static int lines=0;
    private static List<String> words=new ArrayList<String>();

    public static void main(String[] args) {
    File file = new File("C:\\Users\\User\\eclipse\\readFileAllLines\\words.txt"); //Windows path

        try {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                System.out.println(line+" "+lines);
                words.add(lines,line);
                lines++;
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(words.get(0));
    }
}

Now since I have the file such as
Word1 Hint1
Word2 Hint2
etc ...

I am thinking .. I can use substring to read until the space for the word and also substring after the space until the end of the line for the hint?

EDIT: Since substring is going to return the word and the space afterwards, trim() should remove the black space right?

Nvm, tried it all works ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.