Write a program that will read a file of 6 student records called marks.txt, whi
ID: 3633482 • Letter: W
Question
Write a program that will read a file of 6 student records called marks.txt, which you must create yourself as shown below. Each student record consists of a student number (7 digits), a subject (for example "math") and an integer mark.An example of two records would look like this:
2134523
Math
47
7657681
English
98
Your task is to read the file and create two files, one called pass.txt and the other fail.txt. Both output files should contain only subject and mark.
Steps in solving this problem:
1. Using the problem solving process (IPO chart, menu planning, pseudo code for each menu option) analyze this problem.
2. Design a solution - write java on paper for each method
3. Be sure to properly indent and internally document the program (include comments). Submit your program, and marks.txt.
***IMPORTANT- submit with your assignment another file called README.txt, this file will contain descriptions of your subroutines and any important variables you are using.
Explanation / Answer
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class MK1 {
public static void main(String[] args) throws IOException
{
FileInputStream file = new FileInputStream("c:\Marks.txt");
DataInputStream inp = new DataInputStream(file);
BufferedReader input = new BufferedReader(new InputStreamReader(inp));
String strLine;
PrintWriter output1 = null, output2 = null;
File file1 = new File("c:\pass.txt");
File file2 = new File("c:\fail.txt");
output1 = new PrintWriter(new FileWriter(file1));
output2 = new PrintWriter(new FileWriter(file2));
String[] lstLines = new String[18];
int count = 0;
//Read File Line By Line
while ((strLine = input.readLine()) != null)
{
if(strLine.trim().length() >= 1)
lstLines[count++] = strLine;
}
for (int i=0;i<lstLines.length;i = i + 3)
{
output1.println(lstLines[i+1]+" "+lstLines[i+2]);
output1.println("");
output2.println(lstLines[i+1]+" "+lstLines[i+2]);
output2.println("");
}
output2.close();
output1.close();
inp.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.