Advertisement
DigiDuncan

Random Sentence Generator!

Jul 8th, 2015
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 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 RSApp
  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.         //File reader object.
  17.         BufferedReader reader;
  18.         reader = new BufferedReader
  19.                     (new FileReader("C:\\Users\\DigiDuncan\\Desktop\\Java\\Eclipse\\RandomSentence\\src\\wordlist"));
  20.         //TODO: How to make this path relative?
  21.        
  22.         //Set up for a terrible if condition pun.
  23.         int pigs = 1;
  24.         int fly = 2;
  25.        
  26.         //Add each line (which are words) as an object in the ArrayList.
  27.         while (pigs != fly) //Told you.
  28.         {
  29.             String line = reader.readLine();
  30.             //If the line is nothing (EOF), kill the writing process.
  31.             if (line == null)
  32.             {
  33.                 break;
  34.             }
  35.             dict.add(line);
  36.         }
  37.        
  38.         // Close the reader. We don't need it any more.
  39.         reader.close();
  40.  
  41.         //Our input, output, and random generator objects.
  42.         Scanner input = new Scanner(System.in);
  43.         Random generator = new Random();
  44.         StringBuilder builder = new StringBuilder();
  45.        
  46.         //How long should the sentence be? Default value 5-7.
  47.         int sentlength;
  48.        
  49.         //Prompt for sentence length.
  50.         sopl("***Welcome to Gibberer***");
  51.         sopl("*************************");
  52.         sopl("Enter a reasonable sentence length.");
  53.         sop(">");
  54.        
  55.         //Read in the user input.
  56.         sentlength = input.nextInt();
  57.        
  58.         //If the input is stupid, make it less stupid.
  59.         if (sentlength < 1 || sentlength > 100)
  60.         {
  61.             sentlength = (generator.nextInt(5) + 5);
  62.            
  63.             sopl("Unreasonable! Sentence length of " + sentlength + " chosen.");
  64.             sopl("");
  65.         }
  66.        
  67.         //Add words to builder, the amount the user chose.
  68.         for (int i = 0; i < sentlength; i++)
  69.         {
  70.             // Get random index.
  71.             int index = generator.nextInt(dict.size());
  72.            
  73.             //Add a random word to the sentence.
  74.             builder.append(dict.get(index));
  75.            
  76.             //Sometimes, add a comma.
  77.             int randComma;
  78.            
  79.             //Make a random number 1-3.
  80.             randComma = generator.nextInt(3) + 1;
  81.            
  82.             //If the random number is 2, add a comma.
  83.             if (randComma == 2)
  84.             {
  85.                 builder.append(",");
  86.             }
  87.            
  88.             //Add a space after each word.
  89.             builder.append(" ");
  90.         }
  91.        
  92.         // Remove final space and trailing punctuation.
  93.         builder.setLength(builder.length() - 1);
  94.        
  95.         if (builder.charAt(builder.length() - 1) == ',')
  96.         {
  97.             builder.setLength(builder.length() - 1);
  98.         }
  99.        
  100.         //Generate a random int between 0 and 4.
  101.         int punctChoice = generator.nextInt(4);
  102.        
  103.         //Add punctuation to the end of the sentence.
  104.         switch (punctChoice) {
  105.         case 0:
  106.         case 1:
  107.         case 2:
  108.             builder.append(".");
  109.             break;
  110.         case 3:
  111.             builder.append("?");
  112.             break;
  113.         case 4:
  114.             builder.append("!");
  115.             break;
  116.         default:
  117.             builder.append(".");
  118.             break;
  119.         }
  120.        
  121.         // Uppercase the StringBuilder.
  122.         builder.setCharAt(0, Character.toUpperCase(builder.charAt(0)));
  123.  
  124.         // Print result.
  125.         System.out.println(builder);
  126.     }
  127.    
  128.     //Oh god this is hacky. But oh god does it help.
  129.     public static void sop(Object o)
  130.     {
  131.         System.out.print(o);
  132.     }
  133.     public static void sopl(Object o)
  134.     {
  135.         System.out.println(o);
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement