我现在已经看了一段时间了,我确信这个解决方案正好在我面前,但我只是对它视而不见。有人想指出为什么我的int给我一个错误?
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
答案 0 :(得分:1)
您必须在i
块之前声明您的int try-catch
。此外,您必须在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;
}
答案 1 :(得分:0)
我在main,private final int中声明了VERSION在main之外。将其移到main中或将i声明为全局。
static int i=0;
public static void main(String[] args) {
String strFilePath = "./version.txt";
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);
}
}
private final int VERSION = i;