Lenx van Zyl Lenx van Zyl - 12 days ago 5
Java Question

Java: Read text file and add to an Array

After reading a text file and printing it out like so:

public static void main(String[] args) {

File file = new File("/Users/Len/Desktop/TextReader/src/example.txt");
ArrayList<String[]> arrayOfPeople = new ArrayList<>();

try {

Scanner input = new Scanner(file);
while (input.hasNext()) {

String num = input.nextLine();
System.out.println(num);

}
} catch (FileNotFoundException e) {
System.err.format("File Does Not Exist\n");
}
}


Print:

First Name, Surname, Age, Weight, Height
Gordon, Byron, 37, 82, 178
Henriko, Oberholzer, 21, 70, 176
Ruhan, Coetzee, 19, 67, 165
John, Doe, 22, 105, 200
Jack, Frost, 81, 60, 170
Jerry, Joseph, 6, 20, 111


With this data, how do I calculate:

Oldest Person
Youngest Person
Average Weight
Average Height
Average Age


And print it out programmatically.
New to Java.

Answer

You have an ArrayList called "arrayOfPeople", which you do not use yet. While reading your file line by line store the data in your list "arrayOfPeople" and calculate the needed values.

public static void main(String[] args) {
    File file = new File("c:/Users/manna/desktop/example.txt");
    ArrayList<String[]> arrayOfPeople = new ArrayList<>();

    try {
        Scanner input = new Scanner(file);
        input.nextLine();                         //do this to skip the first line (header)
        while (input.hasNext()) {
            String num = input.nextLine(); 
            String[] personData = num.split(","); //returns the array of strings computed by splitting this string around matches of the given delimiter
            arrayOfPeople.add(personData);
        }

        int oldest   = Integer.parseInt(arrayOfPeople.get(0)[2].trim()); //Integer.parseInt("someString") parses the string argument as a signed decimal integer.
        int youngest = Integer.parseInt(arrayOfPeople.get(0)[2].trim()); //String.trim() returns a copy of the string, with leading and trailing whitespace omitted
        double totalWeight = 0;
        double totalHeight = 0;
        double totalAge    = 0;

        for(int i = 0; i< arrayOfPeople.size(); i++){
            String[] personData = arrayOfPeople.get(i);
            if(Integer.parseInt(personData[2].trim())>oldest){
                oldest = Integer.parseInt(personData[2].trim());
            }
            if(Integer.parseInt(personData[2].trim())< youngest){
                youngest = Integer.parseInt(personData[2].trim());
            }

            totalWeight = totalWeight + Double.parseDouble(personData[3].trim());
            totalHeight = totalHeight + Double.parseDouble(personData[4].trim());
            totalAge = totalAge + Double.parseDouble(personData[2].trim());
        }

        System.out.println("Oldest Person: " + oldest);
        System.out.println("Youngest  Person: " + youngest);
        System.out.println("Average Weight: " + totalWeight/arrayOfPeople.size());
        System.out.println("Average Height: " + totalHeight/arrayOfPeople.size());
        System.out.println("Average Age: " + totalAge/arrayOfPeople.size());

    } catch (FileNotFoundException e) {
        System.err.format("File Does Not Exist\n");
    }
} 
Comments