Create an application that enables the user to enter a student’s WIN (no dashes)
ID: 3574862 • Letter: C
Question
Create an application that enables the user to enter a student’s WIN (no dashes), first name, last name, and semester GPA. If the student’s GPA is 2.0 or above his/her information is written to a sequential file named StudentsGoodStanding.txt If the student’s GPA is below 2.0 his/her information is written to a sequential file named StudentsAcademicProbation.txt
Once all information is entered, the program will display both lists read from the files with appropriate headings. The lists that areoutputmust be read from the files and not data in volatile memory
Using CLI in Java.
.
Explanation / Answer
Java Code :
import java.util.Scanner;
import java.io.*;
public class GpaDivision{
private String firstName,lastName;
private double gpa;
public static void main(String []args){
GpaDivision gd = new GpaDivision();
gd.createFiles();
gd.enterDetails();
System.out.println("Students GPA < 2 : ");
gd.readFiles("StudentsAcademicProbation.txt");
System.out.println("Students GPA > 2 : ");
gd.readFiles("StudentsGoodStanding.txt");
}
public void createFiles(){
try{
File myFile = new File("StudentsAcademicProbation.txt");
myFile.createNewFile();
myFile = new File("StudentsGoodStanding.txt");
myFile.createNewFile();
}catch(IOException ioe){
System.out.println("Error : unable to create file");
}
}
public void enterDetails(){
int choice;
do{
Scanner s = new Scanner(System.in);
System.out.println("Enter the Student Details : FirstName , LastName , GPA :");
firstName = s.next();
lastName = s.next();
gpa = s.nextDouble();
if(gpa < 2.0 ){
saveDetails("StudentsAcademicProbation.txt");
}
else{
saveDetails("StudentsGoodStanding.txt");
}
System.out.println("Do you want to continue : 1-yes 2-no ");
choice = s.nextInt();
}while(choice == 1);
}
public void saveDetails(String fileName){
BufferedWriter bufferedWriter = null;
try {
File myFile = new File(fileName);
Writer writer = new FileWriter(myFile,true);
bufferedWriter = new BufferedWriter(writer);
String Content = firstName + " " + lastName + " " + gpa + " ";
bufferedWriter.write(Content);
} catch (IOException e) {
e.printStackTrace();
} finally{
try{
if(bufferedWriter != null) bufferedWriter.close();
} catch(Exception ex){
System.out.println("Error : unable save !");
}
}
}
public void readFiles(String fileName){
try {
FileReader reader = new FileReader(fileName);
int c;
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.