Write a complete program to get data from file name DATA.TXT one line at a time
ID: 3632096 • Letter: W
Question
Write a complete program to get data from file name DATA.TXT one line at a time until there is no more data in that file. The following is one sample line in DATA.TXT ( have as many record as you wish in DATA.TXT)
Name SSN quiz mid assignments participation final
LISA 111-11-1111 100 100 100 100 100
Jack 222-22-2222 80 80 100 90 100
Note that the first line is not in DATA.txt.
Your program should create a file name RESULT.txt that reports Name, SSN and a letter grade according to the following rules:
Total= %15 quiz + %15 mid +%40assignments + %10 Participation+ %final
If total >= 90 then A else if total>=80 then B….
You can print same output to screen as well as RESULT.txt.
Explanation / Answer
package filegrades; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileGrades { public static void main(String[] args) throws IOException { String strInputFile = "c:\DATA.txt"; String strOutputFile = "c:\RESULT.txt"; StringBuilder sbOutput =new StringBuilder(); sbOutput.append("Name SSN Grades").append(System.getProperty("line.separator")); BufferedWriter outputData = null; BufferedReader inputData=null; try { inputData = new BufferedReader(new FileReader(strInputFile)); if(inputData != null) { String line = null; while (( line = inputData.readLine()) != null) { String[] str = line.split(" "); String stuName = ""; String ssn =""; double[] scores; if(str!=null && str.length > 2) { scores = new double[str.length-1]; stuName = str[0]; ssn = str[1]; for(int i=2;i= 90) { Grade = "A"; } else if(TotalScore >= 80 && TotalScore < 90) { Grade = "B"; } else if(TotalScore >= 70 && TotalScore < 80) { Grade = "C"; } else { Grade = "D"; } sbOutput.append(stuName).append(" ") .append(ssn).append(" ") .append(Grade).append(System.getProperty("line.separator")); } } } else { System.out.println("Unable to open input file : " + strInputFile); System.exit(0); } } catch(Exception exp) { System.out.println("Unable to read/process input file : " + strInputFile); System.exit(0); } finally { if(inputData != null) { inputData.close(); } } try { outputData= new BufferedWriter(new FileWriter(strOutputFile)); outputData.write(sbOutput.toString()); } catch(Exception exp) { System.out.println("Unable to open output file : " + strOutputFile); System.exit(0); } finally { System.out.println("" + sbOutput.toString()); if(outputData != null) { outputData.close(); } } } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.