简体   繁体   English

正确的方法是什么? 初始化/未找到符号错误

[英]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.

相关问题 正确的方法是什么? - What is the correct way to do this? 实施此操作的正确方法是什么? - What is the correct way to implement this do while? 编码正确的方法。 Android onCreate和初始化 - Coding the correct way. Android onCreate and initialization 在Struts 2中使用嵌套域对象进行CRUD的正确方法是什么? - What's the correct way to do CRUD with nested domain objects in Struts 2? 枚举字段为Null时ArrayList的元素。 正确的方法是什么? - Elements of ArrayList in Enum field Null. What is the correct way to do this? 在Java中运行执行不同工作的多个线程的正确方法是什么? - What is a correct way to run multiple threads that do different jobs in java? Java UserNotFoundException对符号做什么 - Java UserNotFoundException what to do for the symbol 使用 Java(Spring 框架)和内存数据库测试在 DAO 类中找到的 CRUD 方法的正确方法是什么? - What is the correct way of testing CRUD methods that are found in DAO classes using Java(Spring framework) and In-memory DB? 初始化类和堆栈溢出错误的正确方法 - The correct way to initialize classes and stack overflow errors 在GraphQL-SPQR中返回错误的正确方法 - Correct way to return errors in GraphQL-SPQR
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM