java - Return a random line from a text file -


i want make mad libs program write mad libs template , computer fills in blanks you. i've got far:

package madlibs; import java.io.*; import java.util.scanner;  /**  *  * @author tim  */ public class madlibs {  /**  * @param args command line arguments  */ public static void main(string[] args) throws ioexception {     file nouns = new file("nounlist.txt");     scanner scan = new scanner(nouns);     while(scan.hasnextline()){         if("__(n)".equals(scan.nextline().trim())){             int word = (int) (math.random() * 100);          }     } }  } 

the nounlist.txt file contains list of nouns, each on separate line. question is: how use math.random function choose line used?

get nouns in list, , choose random element out of list.

example:

// nouns contain list of nouns txt file list<string> nouns = new arraylist<>(); random r = new random(); string randomnoun = nouns.get(r.nextint(0, nouns.length)); 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -