请帮我快速修复3个符号未找到的错误程序读取带有两列的输入文本文件并打印出每列的平均值。我有这样的错误:
错误:
AverageOfFloats.java:54: error: cannot find symbol
Float firstValue = new Float(firstToken.parseFloat);
^
symbol: variable parseFloat
location: variable firstToken of type String
AverageOfFloats.java:55: error: cannot find symbol
Float secondValue = new Float(secondToken.parseFloat);
^
symbol: variable parseFloat
location: variable secondToken of type String
AverageOfFloats.java:79: error: cannot find symbol
int lines = avgFloatObject.lines;
^
symbol: variable avgFloatObject
location: class AverageOfFloats
3 errors
代码:
import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;
import java.lang.Float;
/**
* This program prompts a user for a file name with floating point decimal numbers and
* prints the average of each column,
* @author Matthew Miller
*/
public class AverageOfFloats{
/*
* the total of the first column
*/
private int totalFirst;
/*
* the total of the second column
*/
private int totalSecond;
/*
* the lines counted
*/
private int lines;
public AverageOfFloats(){
lines = 0;
totalFirst = 0;
totalSecond = 0;
}
/**
* This main method does all the program logic
* @param args the command line arguments
*/
public void main(String[] args){
try{
// create the class object
AverageOfFloats avgFloatObject = new AverageOfFloats();
// prompt user for input with Scanner class
Scanner userInput = new Scanner(System.in);
System.out.print("Please, enter the file name:");
String fileName = userInput.nextLine();
// read the file path from the users inputted string
File inputFile = new File(fileName);
// close the Scanner stream
userInput.close();
// create the scanner a new scanner object
Scanner reader = new Scanner(inputFile);
// create a temporary string variable in the optimal scope
String line;
while(reader.hasNext()){
// create the object with methods useful for tokens
StringTokenizer token = new StringTokenizer(line);
String firstToken = token.nextToken();
String secondToken = token.nextToken();
Float firstValue = new Float(firstToken.parseFloat);
Float secondValue = new Float(secondToken.parseFloat);
this.totalFirst += firstValue;
this.totalSecond += secondValue;
// consume the line
line = reader.nextLine();
lines++;
}
// close the Scanner stream
reader.close();
}
catch(FileNotFoundException ex){
System.out.println("File not found");
System.out.println(ex.getMessage());
}
catch(IllegalArgumentException ex){
System.out.println("String could not be parsed to float");
System.out.println(ex.getMessage());
}
catch(Exception ex){
System.out.println("Some other exception occured");
System.out.println(ex.getMessage());
}
finally{
}
int lines = avgFloatObject.lines;
float average1 = totalFirst / this.lines;
float average2 = totalSecond / this.lines;
System.out.println("Average of column 1:" + average1 + "/n" + "Average of column 2:" + average2);
}
}
答案 0 :(得分:1)
Float firstValue = new Float(firstToken.parseFloat);
可以更改为
Float firstValue = Float.parseFloat(firstToken);
https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html
在这一行:
int lines = avgFloatObject.lines;
avgFloatObject
的变量声明是:AverageOfFloats avgFloatObject = new AverageOfFloats();
,但这不在范围内。如果您将int lines = avgFloatObject.lines;
的使用移至try
块内,则它将在范围内,因此可以访问。
https://www.geeksforgeeks.org/variable-scope-in-java/
即
移动
int lines = avgFloatObject.lines;
float average1 = totalFirst / this.lines;
float average2 = totalSecond / this.lines;
System.out.println("Average of column 1:" + average1 + "/n" + "Average of column 2:" + average2);
位于reader.close();
行的正下方,位于下一个}
之上(因此位于try
区块内)