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 but not im 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
You are not stuck, thats a good start.
What do you need help with? other then writing a code for you.
Re: Write a Java program that reads file
Quote:
Originally Posted by
Mehwish-S
...but not im stuck... ..
I am guessing that you meant, "now I'm stuck."
Anyhow...
If you have some part of the program working and it doesn't give the output you expect, then one approach to debugging involves putting print statements at strategic points in the program.
When you read a line, print the line.
When you split a string, print all of the resulting strings.
Stuff like that.
Now, if you get to a point where you simply don't understand the way it works (or doesn't work), then here are my suggestions:
- Post a small input file (a couple of lines will be enough to show us the format).
- Post your code that shows where you put the print statements.
- Post the program output. (Paste the output into your post. Don't just tell us that you "got the wrong answer." Show us.)
- Tell us what you expected to happen.
- Tell us what, exactly, you don't understand about the difference between what you expected and what you got.
Cheers!
Z