在文件I / O上遇到一些问题

时间:2014-10-28 18:03:42

标签: java

我有一个家庭作业,从一个文件中读取数据,其中包含每场篮球运动员的名字和分数。该程序应该输出球员的名字和分数,以及每个球员每场比赛的平均得分,并最终显示平均值最高的球员。我目前只是试图获得每个玩家的平均值和换行符。

这是我正在读取数据的输入文件的图片(显然我实际上不能将其作为图像发布,除非我有10个代表,抱歉):

https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/1379989_10152478294323932_1931326649055391994_n.jpg?oh=4a127d414fbb7afaf7488d4531e7664f&oe=54E4B520&gda=1424190489_e403443c6287d05db6f9b9967238ac20

这是我的代码:

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;

public class BasketballTeam
{
    public static void main(String[] args) throws IOException
    {
        File f = new File("BasketballData.txt");

        if (f.exists())
        {
            Scanner input = new Scanner(f);
            int games = 0;
            int totalScore = 0;
            double avg = 0.0;

            while (input.hasNext())
            {
                String s = input.next();
                System.out.printf("%-9s", s);
                int a = input.nextInt();
                while (input.hasNextInt())
                {
                    if (a == -1)
                    {
                        avg = (double)totalScore/games;
                        System.out.printf("%14s%.2f\n", "Average of ", avg);
                        games = 0;
                        totalScore = 0;
                        s = input.next();
                    }
                    else
                    {
                        System.out.printf("%5s", a);
                        games++;
                        totalScore = totalScore + a;
                        a = input.nextInt();
                    }
                }                       
            }
        }
    }
}

当我运行程序时,我的输出只是一行,如下所示:

Smith       13   19    8   12Badgley      5Burch       15   18   16Watson......and so on

为什么我没有获得换行符或平均值?我希望我的输出看起来像这样:

Smith       13   19    8   12   Average of 13
Badgley      5   Average of 5
Burch       15   18   16   Average of 16.33
.....and so on

感谢先进的任何建议/更正。

4 个答案:

答案 0 :(得分:1)

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

public class BasketballTeam
{
    public static void main(String[] args) throws IOException
    {
        File f = new File("BasketballData.txt");

        if (f.exists())
        {
            Scanner input = new Scanner(f);
            int games = 0;
            int totalScore = 0;
            double avg = 0.0;

            while (input.hasNext())
            {
                String s = input.next();
                System.out.printf("%-9s", s);
                while (input.hasNextInt())
                {
                    int a = input.nextInt();
                    if(a != -1)
                    {
                        System.out.printf("%5s", a);
                        games++;
                        totalScore = totalScore + a;
                    }
                }
                avg = (double)totalScore/games;
                System.out.printf("%14s%.2f\n", "Average of ", avg);
                games = 0;
                totalScore = 0;
                System.out.println();
            }
            input.close();
        }
    }
}

这就是你要找的东西。如果你想要除非它是规范的一部分,你甚至不需要文件中每行末尾的-1。它将在没有-1的情况下工作。您只想在内循环之外添加总计的内循环得到平均值并显示。然后重置您的变量。你只需要改变一些东西就很近了。如果您对此如何运作有任何疑问,请随便提出。希望这有帮助!

答案 1 :(得分:0)

我强烈建议您使用FileReader

File file = new File("/filePath");
FileReader fr = new FileReader(file);
Scanner scanner = new Scanner(fr);
//and so on...

答案 2 :(得分:0)

尝试

avg = ((double)totalScore/(double)games);

并将\ n替换为\ r \ n:

System.out.printf("%14s%.2f\r\n", "Average of ", avg);

答案 3 :(得分:0)

在这一行a = input.nextInt()中,您已经前进到下一个int,因此当您达到-1时,测试input.hasNextInt()将为false。

一种可能的解决方案是将循环更改为:

while (input.hasNext()) {
    String s = input.next();
    System.out.printf("%-9s", s);
    int a = 0;
    while (input.hasNextInt()) {
        a = input.nextInt();
        if (a == -1) {
            avg = (double) totalScore / games;
            System.out.printf("%14s%.2f\n", "Average of ", avg);
            games = 0;
            totalScore = 0;
        } else {
            System.out.printf("%5s", a);
            games++;
            totalScore = totalScore + a;
        }
    }
}