Java--找不到符号错误

标签 java symbol-not-found

请帮我快速修复 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);
}
}

最佳答案

FloatfirstValue = new Float(firstToken.parseFloat);

可以改为

FloatfirstValue = Float.parseFloat(firstToken);

https://docs.oracle.com/javase/7/docs/api/java/lang/Float.html

<小时/>

在这一行:

intlines = avgFloatObject.lines;

avgFloatObject 的变量声明为:AverageOfFloats avgFloatObject = new AverageOfFloats();,但这不在范围内。如果将 intlines = avgFloatObject.lines; 的使用移至 try block 内,它将在范围内并因此可访问。

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 block 内)

关于Java--找不到符号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50186465/

相关文章:

java - 无法在 AWS Lambda 中获取 Java 资源文件

java - 为什么 FileWriter 不创建新文件? FileNotFoundException异常

java - 在 Netbeans 中使用文件过滤器

python - 导入错误 : Symbol not found: _futimens with PyQT5 in macOS Sierra 10. 12.6

java - 让两个类在 Java 中协同工作

android - 如何使用Android 3.3加密build.gradle中的密码?在IDE中获取 “Cannot resolve symbol”

java - 像在 Web 浏览器中一样为 JList 平滑滚动

java 。在服务器上搜索输入文本中的单词。实现思路

java - 获取JDBC连接失败;嵌套异常是 java.sql.SQLException : null