So i have a problem that reads: Each line of the file consists of a student’s name followed by an unpredictable number of test
scores. The number of students is also unpredictable. The desired output is as shown where the
numbers there represent the average test score rounded to the nearest whole number.
Create a class called StudentAverages that will input the StudentScores.in text file and produce
the indicated output.
i attached the file that i have to read from.
My code so far is:

import java.io.*; 
import java.util.*; 
public class fdhg

{
public static void main(String args[]) throws IOException
    {
        Scanner sf = new Scanner(new File("Grades.txt"));
        int maxIndx = -1; 
        String text[] = new String[1000]; 

        while(sf.hasNext())
        {
            maxIndx++;
            text[maxIndx]=sf.nextLine();

        }
        sf.close();
        String answer="";
        int sum=0;
        double average=0;
        int n=0;
        String a="";
        for(int j=0;j<=maxIndx; j++)
        {
            Scanner sc=new Scanner(text[j]);

            sum=0;
            String s="";
            n=0;

            while(sc.hasNext())
            {
                int i=sc.nextInt();

                sum=sum+i;
                n=n+1;
            }
            average=sum/n;

            System.out.println(average);
        }


}

}

So i know how to get the averages if there were just numbers in the file but there is a name at the beginning. How do i print out their name with out having any idea what the contents of the file are. thanks for any help you can offer.

Edited 3 Years Ago by Dani: Formatting fixed

Attachments
Agnes 56 82 95 100 68 52
Bufford 87 92 97 100 96 85 93 77 98 86
Julie 99 100 100 89 96 100 92 99 68
Alice 40 36 85 16 0 22 72 
Bobby 100 98 92 86 88

Reading the file with a Scanner is a good start. Now consider the format of you data: you have character strings separated by spaces. This means you could use the .split() method on spaces (" ") to break up your unknown number of scores into a String[]. Remember that the name will always be in index 0, so it will be easy to print. You then simply need to find the average of the rest of the entries in the array. You may need to use the Integer class' parseInt() method as these numbers will be encoded as strings.

Good luck!

ok thanks. regarding the split...im not sure where i should put it or how to how to split the line then convert them back into integers.

This question has already been answered. Start a new discussion instead.