[英]What is the correct way to do this? Initialization/Symbol not found errors
I've been practicing with Scanner and exceptions and they're fairly new so maybe I'm missing something here: 我一直在使用Scanner和异常进行练习,它们非常新,所以也许我在这里遗漏了一些东西:
This first way the compiler says it cannot find filename or f. 编译器说这是找不到文件名或f的第一种方法。
do {
try {
System.out.print("Enter the file name: ");
String filename = k.nextLine();
Scanner f = new Scanner(new File(filename));
done = true;
}
catch(FileNotFoundException ex1){
System.out.println("The file "+filename+" does not exist.");
}
catch(Exception e){
System.out.println("Unpredicted exception");
}
} while (!done);
I thought I'd put it outside to fix it, but this other way it complains f and filename may have not been initialized 我以为我会把它放在外面去修复它,但是以其他方式它会抱怨f和文件名可能尚未初始化
Scanner k = new Scanner(System.in), f;
String filename;
boolean done = false;
// Request file from the user
do {
try {
System.out.print("Enter the file name: ");
filename = k.nextLine();
f = new Scanner(new File(filename));
done = true;
}
catch(FileNotFoundException ex1){
System.out.println("The file "+filename+" does not exist.");
}
catch(Exception e){
System.out.println("Unpredicted exception");
}
} while (!done);
Initialise the value of filename to null. 将filename的值初始化为null。 That way, if the catch block is entered, it will still have a value to use when you're printing the error message. 这样,如果输入了catch块,则在打印错误消息时仍将具有要使用的值。
String filename = null;
You must always declare and initialize variables in such conditions before the 'try' block starts. 在“ try”块开始之前,您必须始终在这种情况下声明和初始化变量。 Otherwise you will have to face with these errors: 'Cannot find the symbol' , 'Variable not initialized' . 否则,您将不得不面对以下错误:'找不到符号','变量未初始化'。
In your code you have declared the String variable outside the 'try' block which is fine, but the problem is that you have not initialized it. 在您的代码中,您已经在'try'块外部声明了String变量,这很好,但是问题是您尚未初始化它。 Hope it solves your problem. 希望它能解决您的问题。
A variable's scope is the block in which it's declared. 变量的作用域是声明它的块。 Your first version declared filename
in the try
block, but you were trying to use it in the catch
block (which is a different block despite being part of the one try-catch
syntactical structure. 您的第一个版本在try
块中声明了filename
,但您试图在catch
块中使用它(尽管它是一个try-catch
语法结构的一部分,但它是一个不同的块。
Your second attempt fixes this by declaring the variable with a scope that includes all places it is used, however in java local variables have no default initialization value; 第二次尝试通过声明变量的作用域(包括使用它的所有位置)来解决此问题,但是在Java中,局部变量没有默认的初始化值。 you must provide a value if there are code paths that could lead to accessing the variable before it has had a value assigned to it. 如果有代码路径可能导致在为变量分配值之前访问该变量,则必须提供一个值。
The fix is to provide any value (including null
) to the variable before the try-catch
., either at declaration time, or in the lines immediately following the declaration. 解决方法是在声明时或声明后紧接的行中,在try-catch
。之前为变量提供任何值(包括null
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.