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: How do I get my program to access and read a file, and put the contents into an array?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default How do I get my program to access and read a file, and put the contents into an array?

    I am given a .txt file of columns of students. Each row contains last name, first name, major, credits completed, and gpa. I need to create a scanner to read from a file, create a printWriter to write to a file. I have the most useless textbook I bought for the class so I pretty much have to learn this on my own. Can someone help me finish this skeleton class?

    Here is my txt file:
    Bunker John Computer Science 42 3.14
    Smith Jane Health Education 32 3.29
    Wilson Robert Community Health 61 2.3
    Clinton Alice Urban Studies 45 2.9
    Podesta Stephanie Biology 92 2.8
    Cronk Anthony Psychology 70 1.9
    Smith Matthew History 33 1.2
    Appletree Joshua Communication Sciences 40 3.4
    Lucas Jill Criminal Justice 56 3.8
    Sanders George Elementary Education 102 2.1
    O'Brien William Nursing 99 3.2
    Newman Lorraine Geography 40 3.1
    Meszaros Mark International Business 56 3.9
    Sedaris Sharon Environmental Sciences 78 3.5
    Cormier Elizabeth Neuro Science 45 3.6
    Peterson Nina Cognitive Science 73 4.0
    Wilson Anna Chemistry 82 3.87

    here is my main method:
    import java.util.Scanner;
    import java.io.*;
     
    /**
     * @author Aparna Mahadev
     * @version 1 - Lab10 Skeleton
     */
    public class Lab10  {
        public static void main (String[] args) throws IOException  {
            // Create a scanner to read from the file
            // TO BE COMPLETED BY THE STUDENT
     
            // create a PrintWriter to write to a file
            // TO BE COMPLETED BY THE STUDENT
     
            Student [] collection = createArrayFromFile(inFile);
     
            // Print out name of each student using a for-each loop
            out.println("Here is the original list");
            out.println("-------------------------\n");
            // TO BE COMPLETED BY THE STUDENT
     
            out.println("\n\nAfter sorting here is the list");
            out.println("------------------------------\n");
            sort(collection);
            // Print out name of each student using a for-each loop
            // TO BE COMPLETED BY THE STUDENT
     
            out.println("\n\nThe lowest GPA among all students is " + findMinGPA(collection, 0, collection.length - 1));
     
            out.println("\n\nThe highest GPA among all students is " + findMaxGPA(collection, 0, collection.length - 1));
     
            inFile.close();
            out.close();
       }
     
       /**
        * Reads one line at a time from inFile;
        * Uses substrging method of String class to parse
        * the line into first name, last name, majors,
        * credits completed and gpa.
        * Creates a Student object and adds it an array
        * Returns the array with exactly the number of Students
        * read
        * @param inFile input file stream
        * @return an array of Students whose length is exactly the  number of
        * students read
        */
       public static Student [] createArrayFromFile(Scanner inFile) {
            // TO BE COMPLETED BY THE STUDENT
        }
     
       /**
        * Sorts the Student array in the increasing order
        * of lastName field.
        * Uses compareTo method of the Student class to
        * compare two student objects
        * @param list An array of Students to be sorted
        */
       public static void sort(Student [] list) {
           // TO BE COMPLETED BY THE STUDENT
        }
     
        /**
         * Finds the minimum GPA in the array recursively
         * Hint: use Math.min method
         *
         * @param list An array of Students
         * @param startIndex the beginning index of the array
         * @param endIndex the end index of the array
         *
         * @return The minimum GPA of all the students in the array
         */
        public static double findMinGPA(Student[] list, int startIndex, int endIndex) {
            // TO BE COMPLETED BY THE STUDENT
        }
     
        /**
         * Finds the maximum GPA in the array recursively
         * Hint: use Math.max method
         *
         * @param list An array of Students
         * @param startIndex the beginning index of the array
         * @param endIndex the end index of the array
         *
         * @return The maximum GPA of all the students in the array
         */
        public static double findMaxGPA(Student[] list, int startIndex, int endIndex) {
            // TO BE COMPLETED BY THE STUDENT
        }
    }

    can someone please just walk me through this? I'm a beginner so if you try to use crazy lingo on me or if you're just extremely broad I might as well just use my textbook.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How do I get my program to access and read a file, and put the contents into an array?

    Create a Scanner instance that reads the file: Scanner (Java Platform SE 7 )
    When you have managed to read the file, start thinking about splitting up the lines: String (Java Platform SE 7 )
    And so on. Do one step after the other.

Similar Threads

  1. Replies: 19
    Last Post: March 15th, 2013, 03:11 PM
  2. Binary File. Extract float data from binary file and put into array
    By cardinal152 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 13th, 2012, 07:31 PM
  3. Using bufferedReader to write contents of text file into an object array.
    By aznenginerd in forum What's Wrong With My Code?
    Replies: 9
    Last Post: May 8th, 2012, 02:13 PM
  4. Read a text file and parse the contents of file
    By HelloAll in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: March 3rd, 2011, 05:47 AM
  5. How to read from all files in a directory & plot the contents?
    By 123 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 11th, 2010, 12:43 PM