6 Replies - 241 Views - Last Post: 22 April 2011 - 10:12 PM Rate Topic: -----

#1 afghanplaye  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 05-March 11

Java Updating Files

Posted 22 April 2011 - 08:27 AM

i got this code. I need some advice on how to change the information and save it back in the file. I am writing this example based on the book and trying to complete it. But i'm stuck.

public class Student
{
	private String firstName;
	private String lastName;
	private String phone;
	private double GPA;
	
	public Student ( )
	{
		firstName = "";
		lastName = "";
		phone = "";
		GPA = 0;
	}

	public Student (String fN, String lN, String pH, double gpa)
	{
		firstName = fN;
		lastName = lN;
		phone = pH;
		GPA = gpa;
	}
	
	public String toString ()
	{
		
		return  "\n\nfirst name: " + firstName +
		"\nlast name: " + lastName + 
		"\nphone: " + phone +
		"\nGPA: " + GPA;
	
	}

}


import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.File;

public class MainClass
{
	public static void main (String [] args)
	{
		final int MAX_SIZE = 20;
		Student [ ] myClass = new Student [MAX_SIZE];
		Scanner inFile = null;
		try
		{
			inFile = new Scanner (new File ("inData.txt"));
		}
		catch (FileNotFoundException ex)
		{
			System.out.println ("file not found");
			System.exit(0);
		}
		
		int size = 0;
		while (inFile.hasNext ())
		{
			String aLine = inFile.nextLine ();
			
			aLine = aLine.trim ();
			int pos = aLine.indexOf(" ");
			String fName = aLine.substring(0,pos);
			aLine = aLine.substring(pos);
			aLine = aLine.trim ();
			pos = aLine.indexOf(" ");
			String lName = aLine.substring (0,pos);
			aLine = aLine.substring(pos);
			aLine = aLine.trim ();
			pos = aLine.indexOf(" ");
			String phone = aLine.substring (0,pos);
			aLine = aLine.substring(pos);
			aLine = aLine.trim ();
			double gpa = Double.parseDouble(aLine);
			
			myClass[size] = new Student (fName,lName,phone,gpa);
					
			size++;
			if (size == MAX_SIZE)
				break;
		}
		
		System.out.println ("data read from file.....");
		for (int i = 0; i < size; i++)
			System.out.print(myClass[i]);
		
		
		System.exit (0);
	}
	

	
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java Updating Files

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Java Updating Files

Posted 22 April 2011 - 08:34 AM

Specifically what is the problem?
Was This Post Helpful? 0
  • +
  • -

#3 Hunterhod  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 21-April 11

Re: Java Updating Files

Posted 22 April 2011 - 08:34 AM

It doesn't look like you've attempted any file output yet. The file section of the Java API should come quite in handy when you do. http://download.orac...1.4.2/docs/api/
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Java Updating Files

Posted 22 April 2011 - 04:45 PM

View PostHunterhod, on 22 April 2011 - 10:34 AM, said:

It doesn't look like you've attempted any file output yet. The file section of the Java API should come quite in handy when you do. http://download.orac...1.4.2/docs/api/

API 1.4.2 ?
You are 5 years late at least
Was This Post Helpful? 0
  • +
  • -

#5 Hunterhod  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 10
  • Joined: 21-April 11

Re: Java Updating Files

Posted 22 April 2011 - 05:27 PM

Apologies, updated link: http://download.orac...ase/6/docs/api/
Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1906
  • View blog
  • Posts: 3,949
  • Joined: 11-December 07

Re: Java Updating Files

Posted 22 April 2011 - 06:46 PM

I've noticed that earlier versions of the documentation appear in google a lot. Tripped me up a couple of times. :)

At least throw the guy a hint. A link to the API is great for someone who knows thir way round it. But even as an experienced programmer, the API for a new language is still daunting!

To the OP, the PrintWriter class is one of the easiest ways of outputting a file. If you can use System.out.println() then a quick look at the PrintWriter class should have you outputting files no problem.
Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10180
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Java Updating Files

Posted 22 April 2011 - 10:12 PM

You can also check out this File I/O tutorial on DIC. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1