-
July 28th, 2012, 08:28 AM
#1
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);
}
}
-
July 28th, 2012, 09:52 PM
#2
Re: Write a Java program that reads file
What are you stuck with? Please be more specific so others can help you quickly.
-
July 30th, 2012, 01:00 PM
#3
Re: Write a Java program that reads file
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
-
August 1st, 2012, 11:04 AM
#4
Re: Write a Java program that reads file
-
August 3rd, 2012, 06:18 AM
#5
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..
-
August 3rd, 2012, 08:12 AM
#6
Re: Write a Java program that reads file
Thank you so much! had to do some alteration but its perfect now
Thanks again!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|