java - 错误: cannot find symbol - Java compile error

标签 java int symbols

我已经研究这个问题有一段时间了,我确信修复就在我面前,但我只是对它视而不见。有人愿意指出为什么我的 int i 给我一个错误吗?

CacheDownloader 类:

  public static void main(String[] args) {
    String strFilePath = "./version.txt";
    try
    {
      FileInputStream fin = new FileInputStream(strFilePath);
     DataInputStream din = new DataInputStream(fin);
     int i = din.readInt();
       System.out.println("int : " + i);
       din.close();
    }
    catch(FileNotFoundException fe)
    {
      System.out.println("FileNotFoundException : " + fe);
    }
    catch(IOException ioe)
    {
      System.out.println("IOException : " + ioe);
    }
  }

    private final int VERSION = i; 

错误:

CacheDownloader.java:54: error: cannot find symbol
        private final int VERSION = i;
                                    ^
  symbol:   variable i
  location: class CacheDownloader

最佳答案

您必须在 try-catch block 之前声明 int i。另外,您必须在 main 方法中声明常量:

  public static void main(String[] args) {
    String strFilePath = "./version.txt";
    int i;
    try
    {
      FileInputStream fin = new FileInputStream(strFilePath);
      DataInputStream din = new DataInputStream(fin);
      i = din.readInt();
      System.out.println("int : " + i);
      din.close();
    }
    catch(FileNotFoundException fe)
    {
      System.out.println("FileNotFoundException : " + fe);
    }
    catch(IOException ioe)
    {
      System.out.println("IOException : " + ioe);
    }
    final int VERSION = i; 
  }

关于java - 错误: cannot find symbol - Java compile error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880396/

相关文章:

java - PWC1406 : Servlet. service() 抛出异常 java.io.IOException: Invalid chunk header 当客户端向服务器发送数据时

java - 如何从 RSA Privatekey.pem 文件中获取 java.security.PrivateKey 对象?

c - 在 C 中,如何将几个 bool 值转换为 int,其位值是不同测试的结果

java - 仍然不确定为什么这个整数保持不变

ios - Long 类型的 NSNumber 对象

c++ - curl_easy_getinfo undefined symbol 错误

java - 找不到符号错误,java似乎认为对象/方法不存在

java - 使用线程打印奇偶数

java - 从 Activity B 打开 Activity A 的特定片段

c++ - 为什么我们通过了链接阶段仍然错过了符号?