Write a Java program that reads file
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);
}
}
Re: Write a Java program that reads file
What are you stuck with? Please be more specific so others can help you quickly.
Re: Write a Java program that reads file
Quote:
Originally Posted by
nambill
What are you stuck with? Please be more specific so others can help you quickly.
Can you please show me your way of how you would write this code & then i will alter mine according to what i need.
Thank you
Re: Write a Java program that reads file
Re: Write a Java program that reads file
Code:
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();
}
}
Should work..
Re: Write a Java program that reads file
Thank you so much! had to do some alteration but its perfect now :)
Thanks again!
Re: Write a Java program that reads file
Quote:
Originally Posted by
Mehwish-S
Thank you so much! had to do some alteration but its perfect now :)
Thanks again!
No problem sir, anytime.