Assignment 17

Task

Write a user-customizable randomizer.

Concepts: command line arguments; file I/O
Textbook: 11.1, 11.4

Steps

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. Or as a random quote displayer: bumperstickers.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\A17>java ZtomaszeA17
No filename (or too many) specified on command line.

Usage: java ZtomaszeA17 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\A17>java ZtomaszeA17 d20.txt
4

D:\TA\grading\A17>java ZtomaszeA17 d20.txt
2

D:\TA\grading\A17>java ZtomaszeA17 d20.txt
10

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

D:\TA\grading\A17>java ZtomaszeA17 d20.txt log.txt
13

D:\TA\grading\A17>java ZtomaszeA17 d20.txt log.txt
20

D:\TA\grading\A17>more log.txt
13
20

D:\TA\grading\A17>java ZtomaszeA17 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 UsernameA17.java 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 (0.5); 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).

FAQs

Do you have any examples of this sort of thing?
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()) {
      System.out.println(filein.nextLine());
    }
  }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.
Reminder
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. Try running your program with hitLocations.txt or bumperstickers.txt or create your own input file.
I want to test my program in situations when it can't write to the output file, but I don't have Word.
Not all programs lock a file when they open it. Notepad, for example, does not. jGrasp probably doesn't either. I know that Microsoft Office and OpenOffice do.

Another way to test this would be to run this program at least once with a second argument in order to create an output file. Then right-click on that output file in Windows Explorer and go to Properties. Click the Read-only box and hit OK. Then try running your program again. You may get a slightly different message, but it should still produce an exception for you to catch.

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.