Programming CycleFileInput - Write in Java with comments Revisit the Cycle class
ID: 3919085 • Letter: P
Question
Programming CycleFileInput - Write in Java with comments
Revisit the Cycle class in Unit 3. Modify your application such that the properties will be read from a text file called “CycleIn.txt”.
Directions
Examine your application for the class called Cycle.
Add an appropriate throws statement in the main method.
Create a reference to a File class with the appropriate name of a text file (Cycle.txt). Note: Cycle.txt was created in the previous assignment, CycleFileOutput.
In your code, check that the text file does exist.
Input the values from the file to memory.
Close the file.
Grading Rubric
Task
Points
Throws clause added in main method
1
Create a reference to the File class and text file
1
Check whether the text file exists
1
Read the properties from the text file and output to screen
1
Close the text file
1
Proper documentation
1
Program works effectively
1
Total
7
Task
Points
Throws clause added in main method
1
Create a reference to the File class and text file
1
Check whether the text file exists
1
Read the properties from the text file and output to screen
1
Close the text file
1
Proper documentation
1
Program works effectively
1
Total
7
Explanation / Answer
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Cycle {
//Throws clause added in main method
public static void main(String[] args) throws FileNotFoundException {
//Create a reference to the File class and text file
File file=new File("CycleIn.txt");
//Check whether the text file exists
if(file.exists()){
Scanner textFile=new Scanner(file);
//Read the properties from the text file and output to screen
while (textFile.hasNextLine()) {
String line = textFile.nextLine();
System.out.println(line);
}
//Close the text file
textFile.close();
}
else{
System.out.println("input file missing check once");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.