dimanche 5 juillet 2020

In java, how do I tell a file to choose and print a random word?

Below is my code that I have so far. I only started programming a couple weeks ago so I am new to all of this, and I cannot figure out how to randomly select and print a word. Where do I start?

public static String randomWord(String fileName) 
 throws FileNotFoundException {
    int fileSize = countWords(fileName);
    int N = (int) (fileSize*Math.random());
    Scanner inFile = new Scanner(new File(fileName));
    String word;
    
 
     
  while (inFile.hasNext()) {
     word = inFile.next();
    }
    inFile.close(); 
    return word;
}



Aucun commentaire:

Enregistrer un commentaire