【发布时间】: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