How to comment this program using java.doc package desroches.bcs345.hwk.schedule
ID: 3669353 • Letter: H
Question
How to comment this program using java.doc
package desroches.bcs345.hwk.schedule.standalonereport;
/**
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* @author Gerard Desroches
*
*/
public class ReadFile {
/**
* @param args
*/
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
System.out.print("Enter input file name:");
String fileName = S.nextLine();
try {
Scanner fileInput = new Scanner(new FileReader(fileName));
System.out.print("Enter output file name:");
fileName = S.nextLine();
PrintWriter outputFile = new PrintWriter(new File(fileName));
String firstName = fileInput.nextLine();
String lastName = fileInput.nextLine();
// The program should always work for a properly formatted input
// file. You should use a
// loop to read in the data. Each time through the loop you should
// process one record from
// the file (read it in, do any necessary calculations, and write it
// out). I may test your
// program with an input file that contains a different number of
// records then the file you
// used as input to your program (for example 20 instead of 12). If
// the program does not
// work on my input file then it will be marked as incorrect.
outputFile.println(" Schedule Report");
outputFile.println(" ---------------");
outputFile.println();
outputFile.println("First: "+firstName);
outputFile.println("Last: "+lastName);
outputFile.println(" M D Y Hr Mn Pd Category Description");
outputFile.println(" - - - -- -- -- -------- ----------- ");
int pmCount=0;
int amCount=0;
while (fileInput.hasNext()) //
{
int Month=fileInput.nextInt();
int Day= fileInput.nextInt();
int Year = fileInput.nextInt();
int Hour = fileInput.nextInt();
int minute = fileInput.nextInt();
fileInput.nextLine();
String period= fileInput.nextLine();
if(period.equals("PM"))
{
pmCount++;
}
else
{
amCount++;
}
String Category = fileInput.nextLine();
String Description = fileInput.nextLine();
outputFile.printf("%02d %02d %4d %02d %02d %2s %-12s %-40s",
Month,Day,Year ,Hour,minute,period,Category,Description );
outputFile.println();
}
outputFile.println();
outputFile.println("Summary");
outputFile.println("-------");
outputFile.println("AM appointments: "+amCount);
outputFile.println("PM appointments: "+pmCount);
fileInput.close();
outputFile.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Explanation / Answer
/**
* Java API
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* @author Gerard Desroches
*
*/
public class ReadFile{
/**
* @param args
*/
@SuppressWarnings("resource")
public static void main(String[] args){
/**
* Ask user for a file to open
*/
Scanner S = new Scanner(System.in);
System.out.print("Enter input file name: ");
String fileName = S.nextLine();
try{
/**
* Open the file specified by user
*/
Scanner fileInput = new Scanner(new FileReader(fileName));
/**
* Ask user for output file
*/
System.out.print("Enter output file name: ");
fileName = S.nextLine();
/**
* Open file Name
*/
PrintWriter outputFile = new PrintWriter(new File(fileName));
/**
* Reading data from file;
*/
String firstName = fileInput.nextLine();
String lastName = fileInput.nextLine();
/**
* Write to another file in proper format
*/
outputFile.println(" Schedule Report");
outputFile.println(" ---------------");
outputFile.println();
outputFile.println("First: "+firstName);
outputFile.println("Last: "+lastName);
outputFile.println(" M D Y Hr Mn Pd Category Description");
outputFile.println(" - - - -- -- -- -------- ----------- ");
int pmCount=0;
int amCount=0;
/**
* Read other data from file
*/
while (fileInput.hasNext()){
int Month = fileInput.nextInt();
int Day = fileInput.nextInt();
int Year = fileInput.nextInt();
int Hour = fileInput.nextInt();
int minute = fileInput.nextInt();
fileInput.nextLine();
String period = fileInput.nextLine();
if(period.equals("PM")){
pmCount += 1;
}
else{
amCount += 1;
}
String Category = fileInput.nextLine();
String Description = fileInput.nextLine();
/**
* Writing Data to file
*/
outputFile.printf("%02d %02d %4d %02d %02d %2s %-12s %-40s",Month,Day,Year ,Hour,minute,period,Category,Description );
outputFile.println();
}
/**
* Writing Data to file
*/
outputFile.println();
outputFile.println("Summary");
outputFile.println("-------");
outputFile.println("AM appointments: "+amCount);
outputFile.println("PM appointments: "+pmCount);
/**
* Close Both file descriptors
*/
fileInput.close();
outputFile.close();
}
catch (FileNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.