0

目前我有存储学生姓名和考试成绩的文本文件。文件的格式是姓氏、名字和考试成绩。文本文件中的每个值都是用空格分隔的。所以文件看起来类似于:

Smith John 85
Swan Emma 75

我已经运行了代码,以便将所有数据打印到控制台,但我不能让它做的是获取所有测试分数,将它们相加,然后找到平均值并将平均值打印到控制台. 以及打印分数低于平均分 10 分的学生。

现在这是我用来读取信息并将信息打印到控制台的代码。

public class ReadTXT 
{

public static void main(String[] args) 
{

{
    String txtFile = "/Users/Amanda/Desktop/Studentdata.txt";
BufferedReader br = null;
String line = "";
String txtSplitBy = " ";

    try {

    br = new BufferedReader(new FileReader(txtFile));
    while ((line = br.readLine()) != null) {


        String[] LastName= line.split(txtSplitBy);
                    String[] FirstName = line.split(txtSplitBy);
                    String[] TS = line.split(txtSplitBy);

        System.out.println("Last Name: " + LastName[0] 
                             + "\n" +"First Name: " + FirstName[1] + "\n" + 
                            "Test Score: " + TS [2] + "\n") ;

    {
        double average = sum / i;
    }
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (br != null) {
        try {
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

System.out.println("Done");

   }

  }
  }

这是我试图用来找到平均值的代码。

 int i =Integer.parseInt ("TS");
double sum = 0.0;
for (int x = 0; x < i; sum += i++);
{
double average = sum / i;
}

不过,我不断收到此异常:

Exception in thread "main" java.lang.NumberFormatException: For input string: "TS"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:492)
    at java.lang.Integer.parseInt(Integer.java:527)
    at ReadTXT.main(ReadTXT.java:35)

我是学习java的新手,但我正在尝试。

4

2 回答 2

1

哎呀:

int i =Integer.parseInt ("TS"); 

这没有意义。“TS”应该代表什么数字?


编辑:你说:

我知道 TS 不是一个有效数字,这就是我试图将其转换为数字的原因

您不能将字母“投射”到数字上,这同样没有意义。

您需要读取文件中的字符串,然后解析这些字符串,而不是您编写的一些字母。


你的代码应该做的是:

  • 在 for 循环中读取文件中的每一行
  • 在这个循环中,使用 String 的split(" ")方法拆分字符串。
  • 用于Integer.parse(...)解析返回的数组中的第 3 项。

那么我会使用 Integer.parse(String [] TS) 吗?

不,这甚至不会编译,因为您试图将 String 数组传递给采用 String 参数的方法。

于 2013-08-11T22:14:35.253 回答
1

这段代码在这里:

String[] LastName= line.split(txtSplitBy);
String[] FirstName = line.split(txtSplitBy);
String[] TS = line.split(txtSplitBy);

并不是真正需要的,因为您要多次拆分行以创建 3 个不同的字符串数组,您要做的是将行拆分一次,然后从数组索引中分配变量,如下所示:

String[] splitLine = line.split(txtSplitBy);
String lastName = splitLine[0];  //first element of array
String firstName = splitLine[1]; //second element of array
String score = splitLine[2];     //third element of array

然后你可以将分数解析为一个字符串,这将是一个数字而不是"TS"你想要的变量名的文字字符串,所以省略"

int i = Integer.parseInt (score);

然后,对于您的 while 循环中的每次迭代,将其加到总数中并进行计数,然后以这种方式计算您的平均值。(或创建分数列表)喜欢:

int total = 0;
int count = 0;
List<Integer> allScores = new ArrayList<Integer>();
while ((line = br.readLine()) != null) 
{
    String[] splitLine = line.split(txtSplitBy);
    String lastName = splitLine[0];  //first element of array
    String firstName = splitLine[1]; //second element of array
    String score = splitLine[2];     //third element of array

    System.out.println("Last Name: " + lastName 
                         + "\n" +"First Name: " + firstname + "\n" + 
                        "Test Score: " + score + "\n") ;

    int i = Integer.parseInt (score);
    total += i;
    count++;
    // Add to List
    allScores.add(i);
}
double average = total/count;

循环列表

for( Integer i: allscores)
{
    // Do the appropriate code here
}

如果您还需要名称,您应该创建一个类来保存名字、姓氏和分数,然后将它们添加到您的列表中。

请参阅本教程

于 2013-08-11T22:17:46.623 回答