简体   繁体   English

“无法解决” Java扫描仪

[英]'in cannot be resolved' Java Scanner

I've recently been learning Java and decided as a little task to understand user input, I would create a times tables generator, for a user entered number. 我最近一直在学习Java,并决定要了解用户输入是一项小任务,因此我将为用户输入的数字创建一个时间表生成器。 Here's the code: 这是代码:

import java.util.Scanner;

public class Tables {

public static void main( String[] args) {

    int IFactor, num, ans;  

    Scanner Input = new Scanner(System.in);
    try {
        System.out.println("Please enter a number to be the factor: ");
        String SFactor = Input.next();
        IFactor = Integer.parseInt(SFactor); 

        num = 1;

        while (num < 11) {   
            ans = num * IFactor;
            System.out.println(num + " * " + IFactor + " = " + ans);
            num++; 
        }

    }
    finally {
        in.close();
    }

}

}

I originally had an error when I was declaring the Scanner 'input' with Eclipse stating that there was a resource leak and that it wasn't closed. 当我使用Eclipse声明扫描仪“输入”时,最初是有一个错误,指出存在资源泄漏并且没有关闭。 I did a bit of research and saw that inserting a try{ } and a finally { } with 'in.close();' 我做了一些研究,发现插入了一个try {}和一个finally {}和“ in.close();”。 would solve the problem. 会解决问题。 However, this wasn't the case as I now have the error: 'in cannot be resolved'. 但是,情况并非如此,因为我现在遇到错误:“无法解决”。

Any help would be much appreciated! 任何帮助将非常感激! Thanks! 谢谢!

in is not assigned to anything. 中未分配任何内容。 You would have to close your scanner called Input. 您将不得不关闭名为Input的扫描仪。

 try{
// code
}
catch(Exception ex)
{
// Exception handling
}
finally{
        if(Input!=null){ 
         Input.close();
        }
}

Your variable name is Input and you are trying to do in.close(). 您的变量名称是Input并且您正在尝试in.close()。 It should be: 它应该是:

finally {
   Input.close();
}

try with resources is the modern / recommended way to close AutoCloseable resources like Scanner . 尝试使用资源是关闭AutoCloseable资源(如Scanner )的现代/推荐方法。 Eg 例如

try (Scanner Input = new Scanner(System.in)) {
    // do stuff with Input
}

and skip the finally block. 并跳过finally块。 Input will be closed when at the end of the try block or earlier if an exception is thrown. try块结束时或更早(如果引发异常),将关闭Input And you don't have to worry about it. 而且您不必担心。

Check Item 7 in Effective Java for reasons to avoid finally block 检查有效Java中的第7项,以避免finally阻塞的原因

Try 尝试

finally {
    Input.close();
}

instead. 代替。 Please note that with java, variable names begin with a lowercase letter generally (and Classes with an upperface) - so it would be better to rename that variable to `input? 请注意,在Java中,变量名通常以小写字母开头(类以大写字母开头)-因此最好将该变量重命名为`input?。 as well. 也一样

The problem is 问题是

finally {
    in.close();
}

You can try this code which uses try-with-resources. 您可以尝试使用try-with-resources的代码。 It is more java8, and you do not need the finally clause: 它是更多的java8,并且您不需要finally子句:

try(Scanner Input = new Scanner(System.in);)
    {
        System.out.println("Please enter a number to be the factor: ");
        String SFactor = Input.next();
        IFactor = Integer.parseInt(SFactor); 

        num = 1;

        while (num < 11) {   
            ans = num * IFactor;
            System.out.println(num + " * " + IFactor + " = " + ans);
            num++; 
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM