Java assignment Write a program that creates three text file

Java assignment:

Write a program that creates three text files : grade1.txt, grade2.txt, grade3.txt

Each contains 5 students’ names (first name, last name) and grades (integers) that are obtained from the keyboard input.

Combine all three files into one text file: allgrades.txt

   -It contains all 15 students\' names and grades from the 3 text file.

   -It also contains letter grades for each student

       If grade < 60, it adds letter grade \"F\"

       If 60 <= grade <70, it adds letter grade \"D\".

       If 70 <= grade < 80, it adds letter grade \"C\".

       If 80 <= grade < 90, it adds letter grade \"B\".

       If 90 <= grade, it adds letter grade \"A\".

   -displays the contents of text file allgrade.txt on the console

Solution

/////////////////////////////////////////////////      Programme Code Begins          ////////////////////////////

import java.util.Scanner;               //scanner class to read input
import java.io.BufferedReader;          
import java.io.*;                       //import io package
public class Grades2                   //main class begins
{
   public static void main(String[] args)throws IOException       //main method begins
   {  
                   BufferedWriter output = null;
                   BufferedWriter allout = null;
                   Scanner sc=new Scanner(System.in);
   //try block for write data to allgrades.txt
               try
               {
                   File ofile = new File(\"allgrades.txt\");
                   allout = new BufferedWriter(new FileWriter(ofile));
   //////////////////Reading data to grade1.txt in try block///////////////////////////////////////
                           try
                           {
                               File file = new File(\"grade1.txt\");
                               output = new BufferedWriter(new FileWriter(file));
                                   for(int i=0;i<5;i++)
                                   {
                                       System.out.println(\"Enter First Name : \ \");
                                       String fname=sc.next();
                                       System.out.println(\"Enter Last Name : \ \");
                                       String lname=sc.next();
                                       System.out.println(\"Enter Grade Name : \ \");
                                       int grade=sc.nextInt();
                                       output.write(fname+\"\\t\"+lname+\"\\t\"+grade+\"\ \");    //write data into grade1.txt
                                       String status=\"\";
                                       //if else ladder to check grade status
                                       if(grade<60)
                                       status=\"F\";
                                       else if(grade>=60 && grade <70)
                                       status=\"D\";
                                       else if(grade>=70 && grade <80)
                                       status=\"C\";
                                       else if(grade>=70 && grade <90)
                                       status=\"B\";
                                       else
                                       status=\"A\";
                                       allout.write(fname+\"\\t\"+lname+\"\\t\"+grade+\"\\t\"+status+\"\ \"); //write grades1.txt data into allgrades.txt

                                   }
                           } catch ( IOException e ) {   e.printStackTrace();         }
                           finally
                           {
                              if ( output != null ) { output.close(); }
                           }


   ////////////////////////////Reading data to grade2.txt/////////////////////////////////
                           try
                           {
                               File file = new File(\"grade2.txt\");
                               output = new BufferedWriter(new FileWriter(file));
                                   for(int i=0;i<5;i++)
                                   {
                                       System.out.println(\"Enter First Name : \ \");
                                       String fname=sc.next();
                                       System.out.println(\"Enter Last Name : \ \");
                                       String lname=sc.next();
                                       System.out.println(\"Enter Grade Name : \ \");
                                       int grade=sc.nextInt();
                                       output.write(fname+\"\\t\"+lname+\"\\t\"+grade+\"\ \");
                                       String status=\"\";
                                       if(grade<60)
                                       status=\"F\";
                                       else if(grade>=60 && grade <70)
                                       status=\"D\";
                                       else if(grade>=70 && grade <80)
                                       status=\"C\";
                                       else if(grade>=70 && grade <90)
                                       status=\"B\";
                                       else
                                       status=\"A\";
                                       allout.write(fname+\"\\t\"+lname+\"\\t\"+grade+\"\\t\"+status+\"\ \");

                                   }
                           } catch ( IOException e ) {   e.printStackTrace();         }
                           finally
                           {
                              if ( output != null ) { output.close(); }
                           }
                          

                           //Reading data to grade3.txt
                           try
                           {
                               File file = new File(\"grade3.txt\");
                               output = new BufferedWriter(new FileWriter(file));
                                   for(int i=0;i<5;i++)
                                   {
                                       System.out.println(\"Enter First Name : \ \");
                                       String fname=sc.next();
                                       System.out.println(\"Enter Last Name : \ \");
                                       String lname=sc.next();
                                       System.out.println(\"Enter Grade Name : \ \");
                                       int grade=sc.nextInt();
                                       output.write(fname+\"\\t\"+lname+\"\\t\"+grade+\"\ \");
                                       String status=\"\";
                                       if(grade<60)
                                       status=\"F\";
                                       else if(grade>=60 && grade <70)
                                       status=\"D\";
                                       else if(grade>=70 && grade <80)
                                       status=\"C\";
                                       else if(grade>=70 && grade <90)
                                       status=\"B\";
                                       else
                                       status=\"A\";
                                       allout.write(fname+\"\\t\"+lname+\"\\t\"+grade+\"\\t\"+status+\"\ \");

                                   }
                           } catch ( IOException e ) {   e.printStackTrace();         }
                           finally
                           {
                              if ( output != null ) { output.close(); }
                           }

               }
               catch ( IOException e ) {   e.printStackTrace();         }
               finally
               {
               if ( allout != null ) { allout.close(); }
               }


               //Display the content of file allgrades.txt
               System.out.println(\"Fname\\tLName\\tGrade\\tStatus\ \");
               try (BufferedReader br = new BufferedReader(new FileReader(\"allgrades.txt\")))
                   {
                       String line = null;
                       while ((line = br.readLine()) != null)   //loop for reading each line in allgrades.txt
                       {
                       System.out.println(line);       //display each line of output file
                       }
                   }
   }
}

Note : i am given same input data after 3 entries.You can enter distinct for this programme.
Output :

Fname   LName   Grade   Status

Ashok   Kumar   99      A
Rebba   Ashok   89      B
A           B         45      F

Ashok   Kumar   99      A
Rebba   Ashok   89      B
A           B         45      F

Ashok   Kumar   99      A
Rebba   Ashok   89      B
A           B         45      F

Ashok   Kumar   99      A
Rebba   Ashok   89      B
A           B         45      F

Ashok   Kumar   99      A
Rebba   Ashok   89      B
A           B         45      F

Java assignment: Write a program that creates three text files : grade1.txt, grade2.txt, grade3.txt Each contains 5 students’ names (first name, last name) and
Java assignment: Write a program that creates three text files : grade1.txt, grade2.txt, grade3.txt Each contains 5 students’ names (first name, last name) and
Java assignment: Write a program that creates three text files : grade1.txt, grade2.txt, grade3.txt Each contains 5 students’ names (first name, last name) and
Java assignment: Write a program that creates three text files : grade1.txt, grade2.txt, grade3.txt Each contains 5 students’ names (first name, last name) and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site