【问题标题】:Errors :Issue with reading text file in class I created错误:在我创建的类中读取文本文件时出现问题
【发布时间】:2013-12-04 07:45:37
【问题描述】:

当我运行我的程序 (BaseballStatsClient) 时,我收到错误:找不到团队名称、最大值和最小值的符号

更新:

我现在在读取文本文件时遇到问题,当我运行程序时,团队名称的输出值为 tars.txt,最大值和最小值为 0.0。我认为在第一个代码中我没有正确读取文本文件

文本文件:

Tars 
0.592
0.427
0.194
0.445
0.127
0.483
0.352
0.190
0.335
0.207
0.116
0.387
0.243
0.225
0.401
0.382
0.556
0.319
0.475
0.279  

这是我创建的类文件,我认为它读取文本文件的方式存在问题:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class BaseballStats {

  private String fileName;
  private String teamName;
  private double [] battingAverage;


  public BaseballStats ( String fileName ) {
    this.fileName = fileName;
    boolean firstLine = true;
    Scanner input = new Scanner(fileName);
    while (input.hasNextLine()) {
      String line = input.nextLine();
      if (firstLine) {
        setTeamName (line);
        firstLine = false;
        continue;
      }
      int i=0;
      while(input.hasNext()) {
        battingAverage[i] = input.nextDouble();
        i++;      
      }
    }
  }

  public String getTeamName( ) {
    return teamName;
  }

  public void setTeamName( String newTeamName ) {
    teamName=newTeamName;
  }

  public double findMaxAverage( ) {
    double max =battingAverage[0];
    for ( int i =1; i < battingAverage.length; i++) {
      if(battingAverage[i] >max)
        max= battingAverage[i];
    }
    return max;
  }
  public double findMinAverage( ) {
    double min =battingAverage[0];
    for ( int i =1; i < battingAverage.length; i++) {
      if(battingAverage[i] < min)
        min= battingAverage[i];
    }
    return min;
  }    
  public double spread( ) {
    //returns the difference between the highest and lowest batting averages
  }
  public int goodPlayers( ) {     
    //returns the number of players with an average higher than .300
  }
  public String toString( ) {  
   // returns a String containing the team name followed by all the batting averages  formatted to three decimal places.
  }
}

这是我正在运行的程序:

import java.util.Scanner;

public class BaseballStatsClient {  
  public static void main( String [] args ) {
    Scanner scan = new Scanner (System.in );
    System.out.println("What is the name of the file you would like to find the statistics of?");
    String fileName = scan.next( );

    BaseballStats newTeam = new BaseballStats(fileName);
    System.out.println("The " + newTeam.getTeamName() + " statistics are:");
    System.out.println("Highest Batting Average: " + newTeam.findMaxAverage( ));
    System.out.println("Lowest Batting Average: " + newTeam.findMinAverage( ));
  }
}

【问题讨论】:

    标签: java class text compiler-errors java-7


    【解决方案1】:
    newTeam.getTeamName( );
    System.out.println("The " + teamName + " statistics are:");
    newTeam.findMaxAverage( );
    System.out.println("Highest Batting Average: " + max);
    newTeam.findMinAverage( );
    System.out.println("Lowest Batting Average: " +min);
    

    您尚未定义定义它们并尝试打印它们。

    用返回值定义变量。

    喜欢

       String teamName =newTeam.getTeamName( );
       System.out.println("The " + teamName + " statistics are:");
    

    剩下的 minmax 变量也是如此。

    而且你可以直接做

     System.out.println("The " + newTeam.getTeamName() + " statistics are:");
    

    注意newTeam,可能是null

    【讨论】:

    • 或者只是System.out.println("The " + newTeam.getTeamName( ) + " statistics are:");,如果不需要验证(在这种情况下最好在 BaseballStats 类中进行)。
    • 好的,谢谢您解决了错误,但它输出团队名称 tars.txt,这是文件的名称,最大值和最小值输出为 0.0
    • 我已经更新了代码,我认为在第一个代码中读取文本文件有问题
    • @user2985542 查看阅读文件中的 if else 条件。这可能是问题所在。
    • 我在看,但我不知道我想看到什么
    【解决方案2】:

    您尚未定义返回值。

    你可以这样做:

    String returnValue =newTeam.getTeamName( );
    System.out.println("The value is " + returnValue);
    

    【讨论】:

    • 好的,我已经解决了这个问题,但我认为文本文件的读取方式存在问题,因为最大值和最小值输出为 0.0,而团队名称是文件名
    猜你喜欢
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    相关资源
    最近更新 更多