In: Computer Science
create a file in java where you can store the first name, last name and the grade of the student. this will repeats until the answer of the question "are you done?" is.equals (y). then write another program that you can read from this file the student's information .
I have dome most of it but my program stores only 1 student's information. WRITE IN FILE
public static void main(String[] args) {
System.out.println("Enter the file name");
Scanner keyboard=new Scanner(System.in);
String fileName= keyboard.next();
boolean done=false;
String response;
PrintWriter output=null;
try {
output=new PrintWriter(fileName);
}
catch(FileNotFoundException e) {
System.out.println ("error in " + fileName);
System.exit(0);
}
while(!done) {
//for (int i=1;i<=2;i++) {
System.out.println("first name:");
String fname= keyboard.next();
System.out.println("Last name:");
String lname=keyboard.next();
System.out.println("Grade:");
double grade=keyboard.nextDouble();
output.println(fname +" "+ lname+" " +grade);
output.close();
System.out.println("Are you done?");
}
String response1 =keyboard.next();
if (response1.equals("y"))
done=true;
}
}
READ THE FILE.
public class ReaderFile {
public static void main(String[] args) throws Exception {
System.out.println("Enter the file name");
Scanner keyboard=new Scanner(System.in);
String fileName=keyboard.next();
File file= new File(fileName);
Scanner input=null;
try {
input= new Scanner(new File(fileName));
}
catch(Exception e) {
System.out.println("Error reading the file" + fileName);
System.exit(0);
}
while(input.hasNext()) {
String fname=input.next();
String lname=input.next();
double grade=input.nextDouble();
System.out.println(fname+" "+lname+" "+grade);
}
}
}
public static void main(String[] args) {
System.out.println("Enter the file name");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.next();
boolean done = false;
String response;
PrintWriter output = null;
try {
output = new PrintWriter(fileName);
}
catch (FileNotFoundException e) {
System.out.println("error in " + fileName);
System.exit(0);
}
while (!done) {
// for (int i=1;i<=2;i++) {
System.out.println("first name:");
String fname = keyboard.next();
System.out.println("Last name:");
String lname = keyboard.next();
System.out.println("Grade:");
double grade = keyboard.nextDouble();
output.println(fname + " " + lname + " " + grade);
System.out.println("Are you done?");
String response1 = keyboard.next();
if (response1.equals("y"))
done = true;
}
output.close();
}
//READ THE FILE.
public class ReaderFile {
public static void main(String[] args) throws Exception {
System.out.println("Enter the file name");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.next();
File file = new File(fileName);
Scanner input = null;
try {
input = new Scanner(new File(fileName));
}
catch (Exception e) {
System.out.println("Error reading the file" + fileName);
System.exit(0);
}
while (input.hasNext()) {
String fname = input.next();
String lname = input.next();
double grade = input.nextDouble();
System.out.println(fname + " " + lname + " " + grade);
}
}
}