0

I am trying to make a class for generating random words. So far my choices are Scanner or a BufferReader I am guessing. This is the code that I think is most efficient however when I run I get null.

Also will a public return randomWord getter grant access to the word in main class?

    private static final String filepath = "/assets/words.txt";
    public String randomWord;
    public Random rand;
    private ArrayList<String> words = new ArrayList<String>();


    public void WordGenerator() {


        rand = new Random();
        String line;

        try {

            InputStream WordsFile = getClass().getResourceAsStream(filepath);
            BufferedReader br = new BufferedReader(new InputStreamReader(WordsFile));
            if(!br.ready()){
                System.out.println("No File");
            }
            else while ((line = br.readLine()) != null) {
                words.add(line);
            }
            br.close();
        }
        catch (IOException e) {
            System.out.println("Something is wrong");
        }

        int size = words.size();
        Random rn = new Random();
        int randWord = rn.nextInt(size);
        randomWord = words.get(randWord);
        System.out.println(randomWord);
    }
}
3
  • Where do you get null null null..? in the println`? Commented May 1, 2016 at 13:33
  • Everything looks good to me... Are you sure the file is loaded properly and that the words.txt file is not empty? Commented May 1, 2016 at 13:37
  • wow that was fast.. thank you.. i get it in the log.. this is a hangman game.. i took the wordgenerator logic and put it in a tester project to test it so i know whats wrong.. the file exists.. i made package called assests put it in core and it automatically moved it in the java folder.. dunno if thats a problem Commented May 1, 2016 at 14:35

1 Answer 1

1

I think what you really need to read your file is to remove the InputStream line and just replace the BufferedReader with this one:

BufferedReader br = new BufferedReader(new FileReader(filepath));

So your code will look like this:

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class WordGeneratorClass
{
   private static final String filepath="../assets/words.txt";
   public String randomWord;
   public Random rand;
   private ArrayList<String> words=new ArrayList<String>();

   public void WordGenerator()
   {
        rand=new Random();
        String line;

        try
        {
           BufferedReader br = new BufferedReader(new FileReader(filepath));

           if(!br.ready())
           {
            System.out.println("No File");
           }
           else while((line=br.readLine())!=null)
           {
              words.add(line);
           }
           br.close();
        }
        catch (IOException e)
        {
           e.printStackTrace();
        }

        int size=words.size();
        Random rn=new Random();
        int randWord=rn.nextInt(size);

        randomWord=words.get(randWord);

        System.out.println(randomWord);
   }

   public static void main(String args[])
   {
      WordGeneratorClass gen = new WordGeneratorClass();
      gen.WordGenerator();
   }
}

Ensure that your assets/words.txt exist.

Edit
Seems that the problem was also related to the path of your words.txt. The above code assumes that the assets/words/words.txt is in the same directory with the source code. For more information, please have a look here.

Sign up to request clarification or add additional context in comments.

5 Comments

this didnt work.. i already know about the shortcut of fileReader but just wanted to be sure.. where do i add the last piece of code?? .. the null doesnt stop btw its is continues which means the loop isnt ending..
hey Rafael thanx for ur help sorry im knew to this forum didnt know replys would be this fast... the problem i think is in finding the path to the file.. if i delete the path way / assests/word.txt i still get the same result which means it cant find the file.. i placed it in the assets folder with the badlogic.jpg.. what do u think might be the problem?.. thanx again
So my code above with path ../assets/words.txt suggests that the words.txt is in a directory called assets in a previous directory. Roughly speaking, If your assets directory is in the same folder with your .java file then you should only use assets/words.txt note that the / is missing in front of the assets. Generally, for more accurate information I will suggest reading more about paths.
Hey Rafael.. just wanted to let you know i figured out the problem thanx to you.. when i called WordGeneratorclass i didnt add the second line.. "gen.WordGenerator();" .. im still new to coding so im guessing some code you just have to know.. : (.. thanx anyway working now
I am glad that this helped. I will suggest to have a read about the paths and relevant paths, to help you understand the concept behind. Also if that solved your problem, you can always mark the question as solved by clicking the "tick" next to the answer that helped you as this will mark that your question has been solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.