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 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 & 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%...
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" -...
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...
java programming write a program with arrays to ask the first name, last name, middle initial,...
java programming write a program with arrays to ask the first name, last name, middle initial, IDnumber and 3 test scores of 10 students. calculate the average of the 3 test scores. show the highest class average and the lowest class average. also show the average of the whole class. please use basic codes and arrays with loops the out put should look like this: sample output first name middle initial last name    ID    test score1 test score2...
Create a Java application that will prompt the user for the first name of 6 friends...
Create a Java application that will prompt the user for the first name of 6 friends in any order and store them in an array. First, output the array unsorted. Next, sort the array of friends and then output the sorted array to the screen. Be sure to clearly label the output. See the example program input and output shown below. It does not have to be exactly as shown in the example, however it gives you an idea of...
2. Create a java program that reads a file line by line and extract the first...
2. Create a java program that reads a file line by line and extract the first word.        The program will ask for the name of input and output file. Input file: words.txt today tomorrow sam peterson peter small roy pratt Output file: outwords.txt tomorrow peterson small pratt
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT