-4

I'm new to Java and this is my first post.

I want to create a program that will print on screen a user-specified number of words that are randomly pulled from an external Notepad file containing several pages of text. Grammar doesn't matter, but every word should be equally likely to be chosen (a way to control word repetition would be nice too, but not essential).

Currently, I have Scanner prompting the user to enter a number, stored as variable "number" determining how many words to pull. The program should then read the text file (or load its contents into some kind of list or array?) and pick a random word of at least one character length. Repeat loop "number" times and display the entire resulting word string.

The parts I need help with are 1) telling the program to access the file; 2) making sure the words are picked randomly. How do I do that?

Many thanks for your attention!

/*
 * Program description: Pulls a user-defined number of random words from an
 * external text file and prints the resulting text string on screen.
 * JDK version 1.7.0_60
 */

import java.util.Scanner; 

public class RandomTextGen {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner (System.in); 
        System.out.print("Enter number of words to pull: ");
        int number = keyboard.nextInt(); 

        // Load text file

        // Create loop to pull number of words in random order

        System.out.println(""); //Output results


    }

}
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
Lotus Flower
  • 1
  • 1
  • 2
  • 1
    post your code so far.... – Jordi Castilla May 26 '15 at 21:48
  • This will help solve your problem: http://stackoverflow.com/questions/12028205/randomly-choose-a-word-from-a-text-file – Jonas Czech May 26 '15 at 21:56
  • Sorry for the delay, this is a personal project in my spare time... thanks for the help so far. As I said, I'm very new to Java, so I don't quite follow everything that is happening with the code in http://stackoverflow.com/questions/136474/best-way-to-pick-a-random-subset-from-a-collection . It's not clear to me how I would incorporate my external text file into that code, or whether it gives me the flexibility to choose how many words to pull without changing the code each time. Sorry to be dense but if someone could break it down and explain what's happening, that'd be great . . . thanks. – Lotus Flower May 31 '15 at 23:12

1 Answers1

2

Read the file and store it into a List

FileInputStream in = new FileInputStream("yourfile.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));

String strLine;
List<String> filearray = new ArrayList<String>();

while ((strLine = br.readLine()) != null) {

    for (int j = 0; j < myarray.length; j++){
        // get the whole line and split into words
        String[] s = br.readLine().split(" ");
        // put each word in the list
        for (String s : strings)
            filearray.add();
    }
}
in.close();

Get the List.size() and pick a random number

int size = filearray.size();
Random rn = new Random();
int randomWord = rn.nextInt(size);

And print it

System.out.println("Random word is: " + filearray.get(randomWord));

NOTE: repeat this as many times as you want...

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109