错误:在我创建的类中读取文本文件时出现问题

时间:2013-12-04 07:45:37

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

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

更新:

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

Txt文件:

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( ));
  }
}

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

您尚未定义返回值。

您可以这样做:

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