namely their gender and their marks in Physics, Chemistry
and Maths, and their phone number. A sample of the file is
given below:
Madvi Pen f 45 56 34 752-2222
Paul Gavin m 78 88 85 696-2315
Write a program that reads information from the above file
and create two files, one file that stores the name, gender,
phone number, and the average mark of all students who have
passed all modules, and one file that the name, gender, phone
number, and the number of passed modules for all students
who have failed at least one module. Assume the pass mark is
40%.
so far i have written this but the code does not write data to file1 and file2. PLEASE HELP
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Formatter;
public class Student {
private static Scanner sc;
private static Formatter file1;
private static Formatter file2;
public static void main(String[]args){
try{
sc=new Scanner(new File("C:/Users/DELL/Desktop/Student.rtf"));
file1=new Formatter("C:/Users/DELL/Desktop/file1.rtf");
file2=new Formatter("C:/Users/DELL/Desktop/file2.rtf");
}
catch(FileNotFoundException fnfe){
System.out.println("cannot create file");
System.exit(1);
}
catch(SecurityException se){
System.out.println("permission denied");
System.exit(1);
}
String fname,lname,gender;
int phy,che,maths;
String tel;
String dummy="";
int avg;
sc=new Scanner(System.in);
while(sc.hasNext()){
fname=sc.next();
lname=sc.next();
gender=sc.next();
phy=sc.nextInt();
che=sc.nextInt();
maths=sc.nextInt();
tel=sc.next();
if(phy>40 && che>40 && maths>40){
avg=(phy+che+maths)/3;
file1.format("%s %s %s %s %d",fname,lname,gender,tel,avg);
}
else {
file2.format("%s %s %s %s %d %d %d",fname,lname,gender,tel,phy,che,maths);
}
dummy=sc.nextLine();
}
sc.close();
if(file1!=null)
file1.close();
}
}
This post has been edited by modi123_1: 22 May 2012 - 09:24 AM
Reason for edit:: highlight the lines THEN click the code tags button in the 'format text' box of the post.

New Topic/Question
Reply




MultiQuote




|