Advertisement
DigiDuncan

Random Sentence v2

Jul 9th, 2015
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. import java.util.Random;
  2. import java.lang.StringBuilder;
  3. import java.util.Scanner;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8.  
  9. public class RSApp2
  10. {
  11.     public static void main(String[] args) throws IOException
  12.     {
  13.         //Make an empty dictionary of words.
  14.         ArrayList<String> dict = new ArrayList<String>();
  15.    
  16.         //Make objects (input, output, reader, and random generator)
  17.         Scanner input = new Scanner(System.in);
  18.         Random generator = new Random();
  19.         StringBuilder builder = new StringBuilder();
  20.         BufferedReader reader = new BufferedReader
  21.             (new FileReader("C:\\Users\\DigiDuncan\\Desktop\\Java\\Eclipse\\RandomSentence\\src\\wordlist"));
  22.         //TODO: How to make this path relative?
  23.        
  24.         int sentlength;
  25.         final int COMMA_FREQ = 3;
  26.         final char[] punct = { '.', '.', '.', '?', '!' };
  27.  
  28.         //Add each line (which are words) as an object in the ArrayList.
  29.         String line;
  30.         while ((line = reader.readLine()) != null)
  31.              dict.add(line);
  32.        
  33.         reader.close();
  34.        
  35.         //Prompt for sentence length.
  36.         sopl("***Welcome to Gibberer***");
  37.         sopl("*************************");
  38.         sopl("Enter a reasonable sentence length.");
  39.         sop(">");
  40.        
  41.         //Read in the user input.
  42.         sentlength = input.nextInt();
  43.        
  44.         //If the input is stupid, make it less stupid.
  45.         if (sentlength < 1 || sentlength > 100)
  46.         {
  47.             sentlength = (generator.nextInt(5) + 5);
  48.            
  49.             sopl("Unreasonable! Sentence length of " + sentlength + " chosen.");
  50.         }
  51.        
  52.         //Add words to builder, the amount the user chose.
  53.         for (int i = 0; i < sentlength; i++)
  54.         {
  55.             // Get random index.
  56.             int index = generator.nextInt(dict.size());
  57.            
  58.             //Add a random word to the sentence.
  59.             builder.append(dict.get(index));
  60.            
  61.             //Sometimes, add a comma.
  62.             if (generator.nextInt(COMMA_FREQ) == 0)
  63.                 builder.append(",");
  64.            
  65.             //Add a space after each word.
  66.             builder.append(" ");
  67.         }
  68.        
  69.         // Remove final space and trailing punctuation.
  70.         builder.setLength(builder.length() - 1);
  71.        
  72.         if (builder.charAt(builder.length() - 1) == ',')
  73.         {
  74.             builder.setLength(builder.length() - 1);
  75.         }
  76.  
  77.         //Add punctuation to the end of the sentence.
  78.         builder.append (punct [generator.nextInt (punct.length) ] );
  79.        
  80.         // Uppercase the StringBuilder.
  81.         builder.setCharAt(0, Character.toUpperCase(builder.charAt(0)));
  82.        
  83.         // Print result.
  84.         sopl("\n" + builder);
  85.        
  86.         //Resource leak! OMAIGOD
  87.         input.close();
  88.     }
  89.    
  90.     //Oh god this is hacky. But oh god does it help.
  91.     public static void sop(Object o)
  92.     {
  93.         System.out.print(o);
  94.     }
  95.     public static void sopl(Object o)
  96.     {
  97.         System.out.println(o);
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement