Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.lang.StringBuilder;
- import java.util.Scanner;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- public class RSApp
- {
- public static void main(String[] args) throws IOException
- {
- //Make an empty dictionary of words.
- ArrayList<String> dict = new ArrayList<String>();
- //File reader object.
- BufferedReader reader;
- reader = new BufferedReader
- (new FileReader("C:\\Users\\DigiDuncan\\Desktop\\Java\\Eclipse\\RandomSentence\\src\\wordlist"));
- //TODO: How to make this path relative?
- //Set up for a terrible if condition pun.
- int pigs = 1;
- int fly = 2;
- //Add each line (which are words) as an object in the ArrayList.
- while (pigs != fly) //Told you.
- {
- String line = reader.readLine();
- //If the line is nothing (EOF), kill the writing process.
- if (line == null)
- {
- break;
- }
- dict.add(line);
- }
- // Close the reader. We don't need it any more.
- reader.close();
- //Our input, output, and random generator objects.
- Scanner input = new Scanner(System.in);
- Random generator = new Random();
- StringBuilder builder = new StringBuilder();
- //How long should the sentence be? Default value 5-7.
- int sentlength;
- //Prompt for sentence length.
- sopl("***Welcome to Gibberer***");
- sopl("*************************");
- sopl("Enter a reasonable sentence length.");
- sop(">");
- //Read in the user input.
- sentlength = input.nextInt();
- //If the input is stupid, make it less stupid.
- if (sentlength < 1 || sentlength > 100)
- {
- sentlength = (generator.nextInt(5) + 5);
- sopl("Unreasonable! Sentence length of " + sentlength + " chosen.");
- sopl("");
- }
- //Add words to builder, the amount the user chose.
- for (int i = 0; i < sentlength; i++)
- {
- // Get random index.
- int index = generator.nextInt(dict.size());
- //Add a random word to the sentence.
- builder.append(dict.get(index));
- //Sometimes, add a comma.
- int randComma;
- //Make a random number 1-3.
- randComma = generator.nextInt(3) + 1;
- //If the random number is 2, add a comma.
- if (randComma == 2)
- {
- builder.append(",");
- }
- //Add a space after each word.
- builder.append(" ");
- }
- // Remove final space and trailing punctuation.
- builder.setLength(builder.length() - 1);
- if (builder.charAt(builder.length() - 1) == ',')
- {
- builder.setLength(builder.length() - 1);
- }
- //Generate a random int between 0 and 4.
- int punctChoice = generator.nextInt(4);
- //Add punctuation to the end of the sentence.
- switch (punctChoice) {
- case 0:
- case 1:
- case 2:
- builder.append(".");
- break;
- case 3:
- builder.append("?");
- break;
- case 4:
- builder.append("!");
- break;
- default:
- builder.append(".");
- break;
- }
- // Uppercase the StringBuilder.
- builder.setCharAt(0, Character.toUpperCase(builder.charAt(0)));
- // Print result.
- System.out.println(builder);
- }
- //Oh god this is hacky. But oh god does it help.
- public static void sop(Object o)
- {
- System.out.print(o);
- }
- public static void sopl(Object o)
- {
- System.out.println(o);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement