mardi 26 mai 2015

Java - Printing random words from a text file

"Hello world," 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


}

}




Aucun commentaire:

Enregistrer un commentaire