Assignment 18


Write a customizable randomizer.

Concepts: command line arguments; file I/O
Textbook: 7.4; 10.6


Write a program that reads in a file and displays a random line of that file. This could be used for a number of uses--as a die roller, to display random quotes, etc. Here are a couple sample files for use as a die roller: d6.txt and d20.txt and hitLocations.txt. Feel free to create your own input files too.

Your program should take at least one filename as a command line argument. This is the file to open and read. If no command line arguments are given (or more than 2), give an error message and explain how to use the program properly.

If a second command line argument is given, treat this as the name of a log file. Your program should append to this log file whatever line it prints to the screen.

Your program should catch all exceptions and explain the source of the error. IOExceptions are pretty good at explaining their cause: call the getMessage() method on the exception you caught to get details you can pass on to your human user.

To test your program, try opening a file that doesn't exist or writing to a file that's in use by a program that locks its files while editing them (such as MS Word).

Sample Output

D:\TA\grading\A18>java ZtomaszeA18
No filename (or too many) specified on command line.

Usage: java ZtomaszeA18 inputfile [outputfile]

This program displays a random line from inputfile.
If outputfile is also given, it also appends the
random line to that file.

D:\TA\grading\A18>java ZtomaszeA18 d20.txt

D:\TA\grading\A18>java ZtomaszeA18 d20.txt

D:\TA\grading\A18>java ZtomaszeA18 d20.txt

D:\TA\grading\A18>java ZtomaszeA18 d20
Could not open file: d20 (The system cannot find the file specified)

D:\TA\grading\A18>java ZtomaszeA18 d20.txt log.txt

D:\TA\grading\A18>java ZtomaszeA18 d20.txt log.txt

D:\TA\grading\A18>more log.txt

D:\TA\grading\A18>java ZtomaszeA18 d20.txt log.txt
Could not open file: log.txt (The process cannot access the file because it is
being used by another process)

This last line generated an exception because I had opened log.txt in another program that locks the files it opens. While log.txt was in use by that other program, I tried running my program and writing to the file--hence the exception.

What to submit

Upload your file to Tamarin.

Grading [5 points]

1 - Compiles + Coding Standards
Your program compiles successfully (no errors). Your code follows Java coding standards.
1 - Command line arguments
Files to read from or write to are passed in at the command line; prints error/usage message on none or more than two args (0.5).
1.5 - Input
Displays a random single line of file given by first command line arg (or appropriate error message if not possible).
1.5 - Output
Appends output to the file given by the second command line arg (if given) (or prints an appropriate error message if not possible).


Do you have any examples of this sort of thing?
Here is an example of command line arguments and writing to a file:
How do I read in from a file?
This code reads in each line of a file and prints it to the screen.
  try {
    Scanner filein = new Scanner(new File("input.txt"));
    while (filein.hasNextLine()) {
  }catch (FileNotFoundException fnfe) {
    System.out.println("Could not open file: " + fnfe.getMessage());
How do I grab a random line of the file?
I recommend you just read the file line by line into an ArrayList. Then use Math.random() to grab a random line from the list.
Remember you're printing a random line from the file, not a random number. Since some people don't bother to create their own textual input files, it's possible to be printing a random number and think you're printing the correct line of d6.txt or d20.txt. So I added a third input file above (for those that still don't want to create their own).
I tried writing to an output file (as the second command line argument) while that file was open in Word (or similar). The exception message printed, but it also first printed out a random line, which the sample output above does not do. Is this a problem?
No, that's fine.

When given two arguments, your program does essentially two things: prints a random line from one file, and writes that line to the second file. It's a question of design whether or not it should do the first thing if it is not going to be able to do the second thing. Either design choice is fine.