0

My project begins with me importing data from a text file and converting it into an ArrayList. I'm not very familiar with ArrayLists and I don't quite know how this will work. I've provided the starter file my instructor has provided and it looks like I'll need to use a buffered reader to import the text file. Because this course has a habit of provided starter code without a good explanation of what that code does I would also appreciate a simple explanation of what a bufferedreader is and how I would create one myself if I had too. Here is the text file as it appears when opened:

manny 89 78 100 91 67
cindy 66 81 94  80 71
timmy 85 59 100 83 21
mikey 88 76 69  82 90
kelly 68 93 63  92 55
sandy 80 73 67  51 99

Here is the starter code:

    import java.util.*;
import java.io.*;

public class ExamScores
{
    public static void main( String args[] ) throws Exception
    {
            // close/reuse this file handle on the next file
            BufferedReader infile = new BufferedReader( new FileReader( "ExamScores.txt" ) );

            // you declare all needed ArrayLists and other variables from here on.

            System.out.println("\nSTEP #1: 50%");  // 50%

            System.out.println("\nSTEP #2: 25%");  // 75 %

            System.out.println("\nSTEP #3: 10%");  // 85%

            System.out.println("\nSTEP #4: 15%"); // 100%

    } // END MAIN

    // - - - - - - H E L P E R   M E T H O D S   H E R E - - - - -


} // END EXAMSCORES CLASS

Once I've created an ArrayList how do I go about manipulating it to do things like sort the names into alphabetical order. Is an ArrayList a lot like a two dimensional array or is each row in that text file going to be a separate array list? To create an idea of everything I need to do in this project I have to sort the names by alphabetical order in an array list. Then I need to compute the average score of each name and finally I need to take the average of the first, third, and fifth exams for each student and find out which of those three exams they scored the lowest average on? I'm not pandering for the exact answer to the project but I want to provide all information for what I'm expected to be able to do with an arraylist at this point. Thank you.

Berylthranox
  • 1
  • 1
  • 1
  • *"this course has a habit of provided starter code without a good explanation of what that code does"* A good tradesman never blames the tools. Around here, trying to elicit help based on doing the same thing usually does not go down well. – Andrew Thompson Nov 17 '13 at 18:00
  • Java is an object oriented language. Learn to define your own classes. You should create a `Row` class (or `Record`, or whatever you want to name it) containing the information contained in a row of your file. Reading the file will then create a `List`. Then you'll google for "How to sort a list of objects in Java". – JB Nizet Nov 17 '13 at 18:03

1 Answers1

1

You can use java.io.BufferedReader to fill the ArrayList with Strings

    BufferedReader inFile = new BufferedReader(new FileReader("ExamScores.txt"));

    //Define and initialise the ArrayList
    ArrayList<String> examScores = new ArrayList<String>(); //The ArrayList stores strings

    String inLine; //Buffer to store the current line
    while ((inLine = inFile.readLine()) != null) //Read line-by-line, until end of file
    {
        examScores.add(inline);
    }
    inFile.close(); //We've finished reading the file        

To access elements in the java.util.ArrayList

    //Loop through all elements in the list and output their values
    for (int i=0; i < examScores.size(); i++)
        System.out.println(examScores.get(i));

Sorting an ArrayList is more difficult. For your program, it's best to parse the String and store the values into a custom StudentExams class. Here's an example I have prototyped for you

    public class StudentExams
    {
        private String studentName;
        private int examScores[];

        //Inline is the String we get from inFile.readLine();
        public StudentExams(String inline)
        {
            //Pesudo-code
            parse the string using String.split(" ");
            store parsed values into studentName and examScores;
        }

        public String getStudentName()
        {
            return studentName;
        }

        public int[] getExamScores()
        {
            return examScores;
        }
    }

Documentation for String#split(String regex)

Tutorial: How to use String#split(String regex)


I haven't given you a direct answer, but I've given you enough information to piece together the answer yourself. It's up to you to read up on all of the tutorials for the aforementioned topics. There's plenty of examples on the internet that do exactly what you're trying to, you just have to try.

Community
  • 1
  • 1
Brett
  • 159
  • 1
  • 8