使用字符串标记器和扫描仪打印出文件输入的学生平均值

时间:2014-04-05 22:20:03

标签: java file-io stringtokenizer

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

  

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
    鲍比100 98 92 86 88

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

  

艾格尼丝,平均= 76
    Bufford,平均= 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 = ");
    }*/
}
}

2 个答案:

答案 0 :(得分:2)

我建议使用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);
    }
}

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

答案 1 :(得分:1)

试试这个:

    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等来得到答案)

    Scanner sf = new Scanner(new File("resources/abc.txt"));
    List<String> text = new ArrayList<String>();
    while (sf.hasNext()) {
        text.add(sf.nextLine());
    }
    sf.close();

    for (String str : text) {
        StringTokenizer sc = new StringTokenizer(str, " ");
        double sum = 0;
        int count = 0;
        String name = sc.nextToken();
        while (sc.hasMoreElements()) {
            sum += Integer.valueOf(sc.nextToken());
            count++;
        }
        System.out.println(name + ", average = " + (sum / count));
    }
}

输出

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