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 RSApp2
- {
- public static void main(String[] args) throws IOException
- {
- //Make an empty dictionary of words.
- ArrayList<String> dict = new ArrayList<String>();
- //Make objects (input, output, reader, and random generator)
- Scanner input = new Scanner(System.in);
- Random generator = new Random();
- StringBuilder builder = new StringBuilder();
- BufferedReader reader = new BufferedReader
- (new FileReader("C:\\Users\\DigiDuncan\\Desktop\\Java\\Eclipse\\RandomSentence\\src\\wordlist"));
- //TODO: How to make this path relative?
- int sentlength;
- final int COMMA_FREQ = 3;
- final char[] punct = { '.', '.', '.', '?', '!' };
- //Add each line (which are words) as an object in the ArrayList.
- String line;
- while ((line = reader.readLine()) != null)
- dict.add(line);
- reader.close();
- //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.");
- }
- //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.
- if (generator.nextInt(COMMA_FREQ) == 0)
- 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);
- }
- //Add punctuation to the end of the sentence.
- builder.append (punct [generator.nextInt (punct.length) ] );
- // Uppercase the StringBuilder.
- builder.setCharAt(0, Character.toUpperCase(builder.charAt(0)));
- // Print result.
- sopl("\n" + builder);
- //Resource leak! OMAIGOD
- input.close();
- }
- //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