Miusheri (miusheri) wrote,

NaNoWriMo the Lazy Geek's Way

50,000 words. That's the goal, ain't it? Well, I'm done, then. And you can be, too! Just find a really big text file, take the code behind the cut, and build/run it.

NOTE: You have to replace the words LESS THAN in the for-loop with the actual less than sign. LJ tries to interpret the less-than sign and everything after it as a tag, hence I had to take it out.

import java.io.*;

public class nanowrimo
// Source of Nano text and file to write Nano to
private String inSource = "C:\\nano\\fulltext.txt";
private String outSource = "C:\\nano\\nano.txt";

// String that will temporarily hold lines from the source file
private String aLine;

// Array that will hold the broken-down contents of source file
private int arraySize = 6000;
String[] storyLines = new String[arraySize];

// Array that will hold individual words from a given line of text
String[] words;

// Word counter
private int numWords = 50000;

// Where all the magic will happen, because I'm lazy
public nanowrimo()
// Step One: Pull out lines of text from the source file
// Create a reader that will access the source file
BufferedReader reader = new BufferedReader(new FileReader(inSource));

// Read in the contents of the input file until our array is full
int index = 0;
while (((aLine = reader.readLine()) != null) && (index < arraySize))
storyLines[index] = aLine;

// Done with the input file, so let's be good programmers and close it up
catch (Exception ex)
// If something goes wrong, lemme know and show me the error
System.out.println("ERROR READING FILE");

// Step Two: Compose our Nano!
// Create a writer to the destination file
BufferedWriter writer = new BufferedWriter(new FileWriter(outSource));

// Now, here's where the magic happens... 50,000 times
int randomNum;
int wordSize;
for (int x=0; x LESS THAN numWords; x++)
// Pick a random number between 0 and the size of our array minus one-
// i.e., select a random line of text
randomNum = (int) (Math.random() * (arraySize - 1));

// Split the random line of text on spaces, and dump the resulting words into our word array
words = storyLines[randomNum].split(" ");

// Figure out how many words we have
wordSize = words.length;

// Pick a random number between 0 and the number of words minus one-
// i.e., select a random word
randomNum = (int) (Math.random() * (wordSize - 1));

// Write the random word to our Nano!
writer.write(" ");

// Clean everything up for next time
words = null;
randomNum = -1;

// Done with the writer, so we should close it!
catch (Exception ex)
System.out.println("ERROR WRITING FILE");

public static void main(String[] args)
// Create a nanowrimo instance and let the constructor do its thing
nanowrimo NNWM = new nanowrimo();

The output by no means makes sense, but coherency is not a stated goal of NaNoWriMo. Word count is.
Tags: geek, programming

  • Share this widely, please.

    Wealth doesn't trickle down - it floods offshore, new research reveals

  • The Story of a Solider - For Memorial Day

    One of the best parts of a film chock-full of best parts. Here's the full song. Bugles are calling from prairie to shore, Sign up and fall in,…

  • Too old for this

    It's been (yeesh) more than seven years since I graduated from Pitt. Why do I still have nightmares along the lines of, "OMG THERE WERE TWO COURSES…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your IP address will be recorded