I need to read the last name and five grades for 10 students from a file and calculate the average of the grades and display that information in a file.
I am pretty sure I know how to tackle this, but I have no clue how to separate the name (String) from the grades (int) in order to average the grades. This is a rough outline as to how I plan to average the grades. It does not work if I leave the name in the inFile.

public static void main(String[] args) throws FileNotFoundException
    {
        Scanner inFile = new Scanner(new FileReader("c:\\InputFile.txt"));

        int sum = 0;
        int num;
        String firstName, lastName;

        while (inFile.hasNext())
        {
            num = inFile.nextInt();
            sum = sum + num;
        }

        sum = sum / 5;
        
        System.out.printf("Sum = %d%n", sum);

I have an idea that arrays would work better for this problem, but we have not covered those yet and I am not familiar with them at all.

Recommended Answers

All 5 Replies

Now assuming your File format is of the type :-

Albert Einstein 0
Stephen Hawking 3

You would need to add the following lines of code in your while loop before reading the grades like :-

.
.
firstName = inFile.next();
lastName = inFile.next();
num = inFile.nextInt();
.
.
.

Note:-
Haven't tested the code but I guess thats how it should work.
You should refer to the javadocs of the Scanner class for more details.

Thanks so much! Below is the code that worked.

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

public class Main
{

    public static void main(String[] args)
    {
        calculateAverage();
    }
    
    public static void calculateAverage()throws FileNotFoundException
    {
        Scanner inFile = new Scanner(new FileReader("c:\\inputfile.txt"));
        int sum = 0;
        int num, avg;
        String lastName;

        lastName = inFile.next();

        while (inFile.hasNext())
        {
            num = inFile.nextInt();
            sum = sum + num;
        }

        avg = sum / 5;
        System.out.println("Last Name: " + lastName);
        System.out.println("The average is: " + avg);
    }

}

Just out of curiosity, how would I write this if there were multiple lines in the input file?
Example:

Smith 56 78 87 98 78
Jones 87 78 65 78 98

I am guessing that I would need a nested control structure, but I am not sure how I would go about that.

There are a few different ways to do it but this would be the easiest.

while (inFile.hasNextLine())
{    	
        int sum = 0;
            
        String name = inFile.next();
        	
        for(int i=0; i<5; i++)
        {
            	sum += inFile.nextInt();
        }
            
        float avg = sum / 5.0f;
                        
        System.out.println("The average for " + name + " is " + avg);
}

Thanks so much! It's always so easy once you see it... =(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.