Back to 111 Main Page

Mobius strip

Assignment 17

Task

Write a customizable randomizer.

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

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. 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

Out of 5 points:

1 - Compiles
1.5 - 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 - 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?
Here is an example of command line arguments and writing to a file: FileAppender.java.
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.


~ztomasze Index : TA Details: ICS111: Assignment 17
http://www2.hawaii.edu/~ztomasze
Last Edited: 11 Nov 2008
©2008 by Z. Tomaszewski.