Programming Assignment 1

File I/O, Console I/O, and Command-Line Arguments

Code Review for XXX XXX

Build Results

  1. Javadoc: Missing comment for public declaration
    csci2912week1.java; line 23

  2. Javadoc: Missing comment for public declaration
    csci2912week1.java; line 25

Requirements Verification

REQ-SP1

All source code should be submitted in one file, and that file should be named according to the following pattern: LastFirstX.java, where Last is the student’s last name, First is the student’s first name, and X is the assignment number.

For example, student John Doe would submit DoeJohn3.java for programming assignment 3.

FAIL. File was submitted as csci2912week1.java.

REQ-SP2

The first lines of the submitted file should include a comment with the following information and format:

/**
 * A short description of the program.
 *
 * @author     Last Name, First Name
 * @assignment CSCI 2912 Assignment X
 * @date       Date
 */

PASS.

REQ-SP3
The submitted file should be emailed to ycheng@hpu.edu.

PASS.

REQ-SP4

The subject line of the email sent to the instructor should follow the pattern: [2912] assignment number.

For example, for assignment 3, student John Doe would write “[2912] assignment 3” as the subject line of his submission email.

PASS.

REQ-SP5
The submission email sent to the instructor should include exactly one attachment, the Java implementation file. Students should not attach any other files to their submission email.

PASS.

REQ-GG1
The implementation file must include JavaDoc for all definitions: classes, interfaces, fields, methods, enumerations, and so on.

FAIL. The implementation contains no JavaDoc.

REQ-A1.1.1
You will implement a Java console application that opens and reads the contents of text files specified as command-line arguments.

FAIL. The application does not process command-line arguments.

REQ-A1.1.2
For each file, the application writes to standard output the name of the file enclosed by square brackets.

PASS.

REQ-A1.1.3
After writing the file name, the application writes the contents of the file with each line prefixed by its corresponding line number, a full colon, and a space.

FAIL. If more than one file is specified, the application displays the wrong line numbers. The counter1 variable should be reset within the for loop.

REQ-A1.2.1
For example, if a file with the name sample-input.txt contains these four lines:
This is the first line.
This is the second line.
This is the third line.
This is the fourth line.
...then the application writes the following output if the user specifies sample-input.txt on the command line:
[sample-input.txt]
1: This is the first line.
2: This is the second line.
3: This is the third line.
4: This is the fourth line.

FAIL. The application does not process command-line arguments.

REQ-A1.3.1
The application processes command-line arguments in the order specified by the user.

FAIL. The application does not process command-line arguments.

REQ-A1.3.2
If the user enters no command-line arguments, the application writes no output for the first part of this assignment.

FAIL. The application asks the user to enter the name of a file (or files) to open.

REQ-A1.3.3
If the user specifies two or more files, the application writes a blank line between the output corresponding to each file.

FAIL. The application does not write a blank line between the output corresponding to each file.

REQ-A1.4.1
The application then reads lines of standard input.

PASS.

REQ-A1.4.2
For each line of input, if the user enters exit, the application terminates; otherwise, the application interprets the line as a path to a text file.

PASS.

REQ-A1.4.3
The application creates or recreates this file and writes to it two lines of output, the name of the file and the current date and time.

FAIL. The application also writes to the file a line number.

REQ-A1.4.4
The application then closes the file, reopens it for reading, and writes its contents to standard output in the same manner as the files in the first part of this assignment.

FAIL. The application does not open the file after creating it.

REQ-A1.5.1
The application writes the following prompt before attempting to read each line from standard input:
Enter a file name or type 'exit' to quit.
-->

PASS.

REQ-A1.5.2
Hint: Be sure to flush standard output after writing the prompt.

FAIL. The application does not flush standard output after writing the prompt.

REQ-A1.6.1
If the application cannot read from standard input, it terminates without displaying an error.

FAIL. The application crashes if standard input is closed.

REQ-A1.6.2
If any I/O errors occur while reading or writing files, the application writes a message to standard error and then continues to process user input.

PASS.

General Observations

Note these observations will not affect the grade.

  1. Writing a method to display the contents of a file would simplify the implementation.

  2. The application does not correctly parse input files with names that contain spaces.

  3. To print the current date and time, it would have been simpler to use the Date class, which was discussed briefly in class and the relevant API was linked twice from the homework assignment.

  4. The implementation is more complex that it needs to be. Please see the example implementation.

  5. Avoid calling System.exit if possible. Calling this method abruptly terminates the JVM and is unsafe to execute in certain contexts. Consider this:

    public class Program {
        public static void main(String[] args) {
            try {
                System.out.println("Calling System.exit()...");
                System.exit(0);
    
                // This will not execute.
                System.out.println("System.exit() returned.");
    
            } finally {
    
                // Warning: This will not execute, either!
                System.out.println("Final block executing...");
            }
        }
    }

Sample Execution

$ java csci2912week1 input-1.txt input-2.txt

Enter filenames, seperated by a space: input-1.txt input-2.txt

[input-1.txt]
1: This is line one.
2: This is line two.
3: This is line three.
4: This is line four.
5: This is line five.
[input-2.txt]
6: This is line one.
7: This is line two.
8: This is line three.
9: This is line four.
10: This is line five.
11: This is line six.
12: This is line seven.

Enter a file name or type 'exit' to quit.
-->output-1.txt

[output-1.txt]
1: output-1.txt
2: Sat Feb 11 22:22:13 HST 2012

Enter a file name or type 'exit' to quit.
-->output-2.txt

[output-2.txt]
1: output-2.txt
2: Sat Feb 11 22:22:13 HST 2012

Enter a file name or type 'exit' to quit.
-->exit

$ java csci2912week1

Enter filenames, seperated by a space: "input 3.txt"

["input]
"input (The system cannot find the file specified)


Enter a file name or type 'exit' to quit.
-->exit

$ java csci2912week1 < /dev/null

Enter filenames, seperated by a space: Exception in thread "main" java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Unknown Source)
        at csci2912week1.main(csci2912week1.java:39)

$ java csci2912week1

Enter filenames, seperated by a space: missing.txt

[missing.txt]
missing.txt (The system cannot find the file specified)


Enter a file name or type 'exit' to quit.
-->.

[.]
1: .
2: Sat Feb 11 22:30:43 HST 2012IO Exception


Enter a file name or type 'exit' to quit.
-->

[]
1: 
2: Sat Feb 11 22:30:43 HST 2012IO Exception


Enter a file name or type 'exit' to quit.
-->exit