0

I'm having trouble trying to figure out what exactly needs to be done in this scenario for the code to work properly. I need to output a text file containing a name and the average of a list of values on each line, the text files contains these things:

Carol,35.00,67.00,13.00
Steve,14.00,82.00,41.00,66.00
Sharon,56.00,42.00,28.00,70.00
Roy,80.00,105.00,55.00
Beatrice,20.00

How do I output the average for each line in this scenario? The code below is an example of a more simpler one with each line only containing one value, I just don't know how to modify the array list or the code to get the values I want.

    import java.util.Scanner;
    import java.io.File;
    import java.util.ArrayList;

    public class AvgCalc {

        public static void main(String[] args) {
            
            File myFile = new File("C:\\Users\\Byoma\\Downloads\\Assignment1.txt");
            
            try {
                
                Scanner myScanner = new Scanner(myFile);
                
                while (myScanner.hasNextLine()) {
                    String line = myScanner.nextLine();
                    
                    String[] tokens = line.split(",");
                    String name = tokens[0];
                    String average = tokens [1];
                    System.out.println("Name: " + name + ", Average: " + average);
                }
                
                myScanner.close();
            }
            
            catch (Exception e) {
                System.out.println("Error reading file: " + myFile.getAbsolutePath());
            }
        }
    }

2 Answers 2

1

Assuming that each of the people that you listed for an example is on a separate line in the text file, adding a for loop to your current code and changing a few lines will solve this for you.

import java.util.Scanner;
import java.io.File;
import java.util.ArrayList;

public class test {

    public static void main(String[] args) {

        File myFile = new File("C:\\Users\\Byoma\\Downloads\\Assignment1.txt");

        try {

            Scanner myScanner = new Scanner(myFile);

            while (myScanner.hasNextLine()) {
                String line = myScanner.nextLine();

                String[] tokens = line.split(",");
                String name = tokens[0];
                double sum = 0; //Initialized a double to sum the values
                for (int i = 1; i < tokens.length; i++) {
                    sum += Double.parseDouble(tokens[i]); //Parse the values in the text document as doubles
                }
                double average = sum / (tokens.length - 1); //Get the average by dividing the sum by the number of values
                System.out.println("Name: " + name + ", Average: " + average);
            }

            myScanner.close();
        }

        catch (Exception e) {
            System.out.println("Error reading file: " + myFile.getAbsolutePath());
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You iterate through the elements of the array starting with element 1 and accumulate them, then divide the total sum of the elements by the number of elements (note that I used int for the accumulated sum, but you can use floats or doubles if your input is not integer values, and that you don't strictly need the count variable):

public class MyClass {
    public static void main(String args[]) {
        String[] array = {"Name", "11", "5", "107"};
        int accumulated = 0; //this will hold the sum of all the numbers
        int count = 0; //we use this to keep track of how many numbers we have
        for (int i = 1; i < array.length; i++) {
            //add the numbers, we convert them to int since they are strings
            accumulated = accumulated + Integer.parseInt(array[i]); 
            count++;
        }  
        System.out.println("Total sum: "+accumulated);
        System.out.println("Number of elements: "+count);
        float average = accumulated/count;
        System.out.println("Average: "+average);
    }
}

Output:

Total sum: 123
Number of elements: 3
Average: 41.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.