Java Madlibs Program

Matrica Kovolunchy

Greenhorn

Posts: 18

posted 4 years ago

1

Hi. I am trying to create a java program that will do madlibs. This program must, take in a file through a command line argument, find "variables within a written text, then rewrite the file and save it as an output. I know that was wordy, so let me demonstrate.

Say I have a text file called input.txt and within it lies:
I once was a crazy [adjective] and [decision] to
divide [number] by 0.

If I call java lala input.txt output.txt

The program must take the input file (input.txt) and list "adjective", "decision" and "number", ask for an input. Then the program must rewrite the text with the inputs a user gave it and store it in output.txt.

I am trying to write accurate pseudocode before I actually start programming. How does this look?

Read the values of the arguments and store them as two variables.
If there are more than two arguments in the argument line throw an error and quit the program
Read the text from the file defined in command line argument 0
Put the values "defined" between [ ] into an arraylist and end the read when there are no more lines left <----How would I go about this?
Use a loop to store values into the respective indexes of the arraylist where the values are defined and end the loop when done with values.
Reprint the input text file with the values in the arraylist and save it to the value defined in command line arg 1 <----Don't know how to go about this.

Sorry for the long post, but I thought it would be worth explaining. Thank you for your time!

Stuart A. Burkett

Ranch Hand

Posts: 679

posted 4 years ago

The Java I/O tutorial is probably a good place to start learning about reading from and writing to files.
Personally I'd swap the first two steps around. That way you can also check that there aren't less than two arguments. If there were and you tried to assign the values to variables you would get an exception. Apart from that it looks like a good approach.

Matrica Kovolunchy wrote:I am trying to write accurate pseudocode before I actually start programming. How does this look?

As Stuart said: Looks like a good start; and it's great to see that you're doing it (hence the +1 on your post).

One thing: You haven't said which order you prompt for the input (ie, in the order they appear, or by type of word), and whether there is anything different about the prompt for an 'adjective' and the prompt for a 'decision'.
Just because you're writing pseudo-code doesn't mean that you can afford to be "woolly" about these things.

Writing good pseudo-code is a bit of a black art, and I know many experienced programmers (myself included) to whom it doesn't come naturally; but the mere fact that you're starting now is a great sign.

Keep it up.

Winston

"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here