Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help reading data from file, manipulating then rewriting out.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Help reading data from file, manipulating then rewriting out.

    I'm trying to figure out how to search for a specific student by first name and then change their specific data and finally write it back out to the same file keeping all other data unchanged. I'm not sure how to start the methods or what the correct algorithms would be for achieving this. The code so far looks like this:

    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);
    	}
     
    	public static void findName(String name){
     
     
     
    	}
     
    }

     
    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;
     
    	}
     
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Help reading data from file, manipulating then rewriting out.

    You will need to show us an example of the input file.

    Also, what currently happens when you run the program? Does any of it work?
    Are there any exceptions thrown? Which part are you stuck on exactly?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  2. Replies: 8
    Last Post: March 25th, 2011, 02:34 PM
  3. Reading file Data into a "struct"
    By Brandt in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 9th, 2011, 10:02 AM
  4. Help Please - Rewriting text
    By dechno in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: January 21st, 2011, 03:00 PM
  5. Reading Chunks of Data
    By icu222much in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 22nd, 2010, 08:39 PM