Java 使用字符串标记器和扫描器通过文件输入打印学生平均数

Java 使用字符串标记器和扫描器通过文件输入打印学生平均数,java,file-io,stringtokenizer,Java,File Io,Stringtokenizer,我试图得到文本文件中的平均值。该文件的内容是: Agnes 56 82 95 100 68 52 布福德879297100968593779886 朱莉99 100 100 89 96 100 92 99 68 爱丽丝40 36 85 16 0 22 72 博比100 98 92 86 88 我必须跳过名称,并尝试对每行整数的值求和。输出应该是这样的: Agnes,平均值=76 布福德,平均值=91 朱莉,平均值=94 爱丽丝,平均值=39 鲍比,平均值=93 我的问题是无法对值求和(使用sum

我试图得到文本文件中的平均值。该文件的内容是:

Agnes 56 82 95 100 68 52
布福德879297100968593779886
朱莉99 100 100 89 96 100 92 99 68
爱丽丝40 36 85 16 0 22 72
博比100 98 92 86 88

我必须跳过名称,并尝试对每行整数的值求和。输出应该是这样的:

Agnes,平均值=76
布福德,平均值=91
朱莉,平均值=94
爱丽丝,平均值=39
鲍比,平均值=93

我的问题是无法对值求和(使用sum+=sc1.nextInt()。我也不能只计算整数的标记数。例如,对于第一行,我需要countTokens等于6,但我得到7,即使跳过了名称

import java.io.*; 
import java.util.*; 
public class studentAverages
{
public static void main() throws IOException
{
    Scanner sf = new Scanner(new File("C:\\temp_Name\\StudentScores.in"));
    int maxIndex = -1; 
    String text[] = new String[100]; 
    while(sf.hasNext( ))
    {
        maxIndex++;
        text[maxIndex] = sf.nextLine();
    }
    sf.close();

    int sum=0;
    int avg=0;
    int divisor=0;
    for (int i=0;i<=maxIndex; i++)
    {
        StringTokenizer sc= new StringTokenizer(text[i]);
        Scanner sc1= new Scanner (text[i]);

        while (sc1.hasNext())
        {
            sc1.useDelimiter(" ");
            sc1.skip("\\D*");                
            System.out.print(sc1.nextInt());
            System.out.println(sc1.nextLine());
            sum+=sc1.nextInt(); // trying to sum all the numbers in each line, tried putting this line everywhere
            avg=sum/divisor;
            break;
        }            
        System.out.println(avg);
        while (sc.hasMoreTokens())
        {         
            divisor=sc.countTokens()-1; //Not able to count tokens of just the numbers, thats why I am using -1 
            //System.out.println(divisor);
            break;
        }
    }
    //this is for the output 
    /*for (int i=0; i<=maxIndex; i++)
    {
        String theNames="";
        Scanner sc= new Scanner (text[i]);
        theNames=sc.findInLine("\\w*");
        System.out.println(theNames + ", average = ");
    }*/
}
}
import java.io.*;
导入java.util.*;
公立学生公寓
{
公共静态void main()引发IOException
{
Scanner sf=新的扫描仪(新文件(“C:\\temp\u Name\\StudentScores.in”);
int maxIndex=-1;
字符串文本[]=新字符串[100];
while(sf.hasNext())
{
maxIndex++;
text[maxIndex]=sf.nextLine();
}
sf.close();
整数和=0;
int平均值=0;
整数除数=0;
对于(int i=0;i请尝试以下方法:

    Scanner scanner = new Scanner(new File("resources/abc.txt"));
    //check for next line
    while (scanner.hasNextLine()) {
        //create new scanner for each line to read string and integers
        Scanner scanner1 = new Scanner(scanner.nextLine());
        //read name
        String name = scanner1.next();
        double total = 0;
        int count = 0;
        //read all the integers
        while (scanner1.hasNextInt()) {
            total += scanner1.nextInt();
            count++;
        }
        System.out.println(name + ", average = " + (total / count));
    }
    scanner.close();
输出:

Agnes, average = 75.5
Bufford, average = 91.1
Julie, average = 93.66666666666667
Alice, average = 38.714285714285715
Bobby, average = 92.8

--编辑--

这是根据您最后一条评论编写的代码(我必须使用StringTokenizer`/string方法,如useDelimiter、skip等,才能得出答案)


我建议使用
split
方法拆分每一行,然后在忽略第一行的同时循环这些值,因为您知道这就是标题

public static void main() throws IOException {
    Scanner sf = new Scanner(new File("C:\\temp_Name\\StudentScores.in"));
    int maxIndex = -1; 
    String text[] = new String[100]; 

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

    sf.close();

    for(int i = 0; i < maxIndex; i ++) {
        String[] values = text[i].split(" ");
        String title = values[0];
        int sum = 0;

        for(int j = 1; i < values.length; j ++) {
            sum += Integer.parseInt(values[j]);
        }

        double average = sum / (values.length - 1);
        System.out.println(title + ": " + average);
    }
}
publicstaticvoidmain()引发IOException{
Scanner sf=新的扫描仪(新文件(“C:\\temp\u Name\\StudentScores.in”);
int maxIndex=-1;
字符串文本[]=新字符串[100];
while(sf.hasNext()){
maxIndex++;
text[maxIndex]=sf.nextLine();
}
sf.close();
对于(int i=0;i

请注意,内部循环的索引从1开始,而不是从0开始,在计算平均值时,我们从
数组的大小中减去1,因为我们想忽略标题。

这是一个很好且简单的解决方案!效果很好,但我必须使用StringTokenizer/string方法,如useDelimiter、skip等来arri我知道答案了。有什么建议吗?为什么你只想使用
StringTokenizer
?通过使用
Scanner
,你可能已经在使用了。保持简单。这正是我想要的。谢谢!这也行得通,但我需要使用string方法/string tokenizer来获得答案
public static void main() throws IOException {
    Scanner sf = new Scanner(new File("C:\\temp_Name\\StudentScores.in"));
    int maxIndex = -1; 
    String text[] = new String[100]; 

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

    sf.close();

    for(int i = 0; i < maxIndex; i ++) {
        String[] values = text[i].split(" ");
        String title = values[0];
        int sum = 0;

        for(int j = 1; i < values.length; j ++) {
            sum += Integer.parseInt(values[j]);
        }

        double average = sum / (values.length - 1);
        System.out.println(title + ": " + average);
    }
}
public static void main() throws IOException {
    Scanner sf = new Scanner(new File("C:\\temp_Name\\StudentScores.in"));
    int maxIndex = -1; 
    String text[] = new String[100]; 

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

    sf.close();

    for(int i = 0; i < maxIndex; i ++) {
        String[] values = text[i].split(" ");
        String title = values[0];
        int sum = 0;

        for(int j = 1; i < values.length; j ++) {
            sum += Integer.parseInt(values[j]);
        }

        double average = sum / (values.length - 1);
        System.out.println(title + ": " + average);
    }
}