7 Replies - 356 Views - Last Post: 23 May 2012 - 04:59 PM Rate Topic: -----

#1 yu8690  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-November 10

READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 22 May 2012 - 06:47 AM

The file student.txt stores information about HSC students,
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.


Is This A Good Question/Topic? 0
  • +

Replies To: READ DATA FROM FILES AND WRITE DATA TO FILES

#2 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 369
  • Joined: 12-March 12

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 22 May 2012 - 07:45 AM

I may be wrong, but you don't have any form of File Writer's in your code. Which is probably why you aren't getting any file output.
Was This Post Helpful? 1
  • +
  • -

#3 yu8690  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-November 10

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 22 May 2012 - 07:57 AM

thanks for replying!!wer should i include the filewriter?
Was This Post Helpful? 0
  • +
  • -

#4 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 369
  • Joined: 12-March 12

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 22 May 2012 - 06:16 PM

Implement it at the top as such
FileWriter fw = new FileWriter(new File(FILE));

If you would like the file to be overwritten each time it's executed, or you could do it as below, and have the file just update when it's executed
FileWriter fw = new FileWriter(FILE, true));

Then inside your code, where you want to print out to a file, use this
fw.write("What you want to write");

But you need to make sure you always close it, otherwise nothing will be output to the file
fw.close();

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,125
  • Joined: 06-March 08

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 23 May 2012 - 02:04 PM

View PostILoveJava, on 22 May 2012 - 10:45 AM, said:

I may be wrong, but you don't have any form of File Writer's in your code. Which is probably why you aren't getting any file output.

Yes you are wrong :). A Formatter will do it for you

   	 Formatter f = new Formatter("foo.txt");
    	 f.format("This is a number %d\n", 10);
    	 f.close();


will write "This is a number 10" in foo.txt

http://docs.oracle.c.../Formatter.html


You are complicationg the OP code for nothing :)

To the OP you have to close file1 and file2. And no need to test, they can't be null

This post has been edited by pbl: 23 May 2012 - 02:05 PM
Reason for edit:: Added Formatter link

Was This Post Helpful? 2
  • +
  • -

#6 yu8690  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-November 10

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 23 May 2012 - 02:12 PM

thanks a lot for your help!!it works now:)
Was This Post Helpful? 0
  • +
  • -

#7 ILoveJava  Icon User is offline

  • D.I.C Regular

Reputation: 28
  • View blog
  • Posts: 369
  • Joined: 12-March 12

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 23 May 2012 - 04:48 PM

I knew i may have been wrong. Haha.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8017
  • View blog
  • Posts: 31,125
  • Joined: 06-March 08

Re: READ DATA FROM FILES AND WRITE DATA TO FILES

Posted 23 May 2012 - 04:59 PM

View PostILoveJava, on 23 May 2012 - 07:48 PM, said:

I knew i may have been wrong. Haha.

No problem... actually probably the first question about this almost unknown class in this forum. It was introduced with JRE 1.5 to have an as easy to use output counterpart as Scanner is for input.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1