Write a Java program that reads the file "X", and prints to the console the file update time, the list of marks for a given UB number and the corresponding “marks gained” value.
So far I have got this and not im totally stuck :/
Help!
Code:
package textcopy;
import java.io.*;
/**
* This class demonstrates the number of students matching the UB number criterion and the average mark.
*
* @author MSheraz3
*/
public class TextCopy {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(args[0]));
BufferedWriter writer = new BufferedWriter(new FileWriter(args[1]));
String line = null;
while ((line=reader.readLine()) != null)
if ( line.split(",")[0].matches("\\d*7\\d*") )
{ writer.write(line); writer.newLine(); }
reader.close(); writer.close();
double sum=0; int counter=0;
{ sum = sum+Integer.parseInt(line.substring(9)); counter = counter+1; }
System.out.println("Students: "+counter+"\nAverage mark: "+sum/counter);
}
}
public static void Save() {
File file = new File("Save.txt"); //Output files name
try {
Writer output = null;
output = new BufferedWriter(new FileWriter(file));
output.write(""); //Writes the File
output.close(); //Closes the output
//System.out.println("");
} catch(IOException e) {
e.printStackTrace();
}
}
public static void Read() {
try {
File file = new File("Save.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
while ((line = reader.readLine()) != null) {
//System.out.println(line);
}
reader.close();
reader = null;
} catch (FileNotFoundException e) {
//The error should not show up, as of if the file doesn't exist, the void should just ignore it.
} catch (IOException e) {
e.printStackTrace();
}
}
Bookmarks