Question

In: Computer Science

create a file in java where you can store the first name, last name and the...

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);

}

}

}

Solutions

Expert Solution

   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);

       }

   }

}


Related Solutions

Name your file “DMx1_Your last name_Your first name.” You can use MS Word to complete the...
Name your file “DMx1_Your last name_Your first name.” You can use MS Word to complete the matrices (go to Insert, Table, Insert table, fill the table). Save the file as a PDF file. If you draw the matrices on paper, take pictures. Make sure the pictures are clear and readable. Insert the pictures in MS Word document and save it as a PDF file. 1. Align using dot matrix: horizontal sequence – AGGCTCCC, vertical sequence – GCGCTCCG. Trace and explain...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All...
JAVA PROGRAMMING Part 1 Create a class Student, with attributes id, first name, last name. (All the attributes must be String) Create a constructor that accepts first name and last name to create a student object. Create appropriate getters and setters Create another class StudentOperationClient, that contains a main program. This is the place where the student objects are created and other activities are performed. In the main program, create student objects, with the following first and last names. Chris...
create a java class with name ModernArt: For the first part of the assignment, you will...
create a java class with name ModernArt: For the first part of the assignment, you will be extending the JApplet class and creating a work of modern using the Graphics class to draw various shapes to the applet window. As part of your masterpiece, you should incorporate the following elements: At least 1 non-solid rectangle At least 1 non-solid oval At least 1 solid oval At least 1 non-solid Polygon At least 1 solid Polygon At least 3 lines At...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
The program reads a text file with student records (first name, last name and grade on...
The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). <--- Completed Need help on this Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok" - prints students with grade <= 89. "end" -...
Create & name a file with the following format: LastNameFirstNameUnit5.java. Example: The instructor would create a...
Create & name a file with the following format: LastNameFirstNameUnit5.java. Example: The instructor would create a file with the following name: TonsmannGuillermoUnit5.java Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter. Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. Comments REQUIRED; flow charts & pseudocode NOT REQUIRED. 5%...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name,...
C# (Thank you in advance) Create an Employee class with five fields: first name, last name, workID, yearStartedWked, and initSalary. It includes constructor(s) and properties to initialize values for all fields. Create an interface, SalaryCalculate, class that includes two functions: first,CalcYearWorked() function, it takes one parameter (currentyear) and calculates the number of year the worker has been working. The second function, CalcCurSalary() function that calculates the current year salary. Create a Worker classes that is derived from Employee and SalaryCalculate...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname10.java, using your last name and your first name, that does the following: Create two arrays that will hold related information. You can choose any information to store, but here are some examples: an array that holds a person's name and an array that hold's their phone number an array that holds a pet's name and an array that holds what type of animal that pet is an array that holds a student's...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name,...
Using jGRASP, write a Java program named LastnameFirstname09.java, using your last name and your first name, that does the following: Declare an array reference variable called myFavoriteSnacks for an array of String type. Create the array so that it is able to hold 10 elements - no more, no less. Fill the array by having each array element contain a string stating one of your favorite foods/snacks. Note: Only write the name of the snack, NO numbers (i.e. Do not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT