从另一种方法在 Try{} 中调用 Scanner? --Java
我的问题在于尝试从另一种方法访问在一种方法中创建的扫描仪扫描。它说找不到变量扫描。我尝试声明全局扫描仪扫描
,但它给了我一个错误,无法从静态上下文引用非静态变量名称
。我怎样才能访问这个变量?
import java.util.*;
import java.io.*;
public class MyClass {
public static void myMethod() {
final File f = new File("file.txt");
Scanner scan = null;
try {
scan = new Scanner(f);
}
catch(FileNotFoundException ex) {
System.exit(0);
}
}
public static boolean anotherMethod() {
final String s = scan.next ();
if (s.equalsIgnoreCase ("true")) return true;
if (s.equalsIgnoreCase ("false")) return false;
throw new java.util.InputMismatchException ();
}
}
My problem lies with trying to access Scanner scan
created in one method from another method. It says it cannot find variable scan. I tried declaring a global Scanner scan
, but it gave me an error, non static variable name cannot be referenced from a static context
. How can I access this variable?
import java.util.*;
import java.io.*;
public class MyClass {
public static void myMethod() {
final File f = new File("file.txt");
Scanner scan = null;
try {
scan = new Scanner(f);
}
catch(FileNotFoundException ex) {
System.exit(0);
}
}
public static boolean anotherMethod() {
final String s = scan.next ();
if (s.equalsIgnoreCase ("true")) return true;
if (s.equalsIgnoreCase ("false")) return false;
throw new java.util.InputMismatchException ();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您仅在
flow
方法中声明了scan
变量。如果您想在其他方法中使用该值,则需要采取以下两种操作之一:由于所有方法都是静态的,对于您需要将其声明为的第一个选项:
但是,目前
flow
方法似乎做了两件完全不同的事情:Scanner
(但实际上并不使用它)在我看来,您应该在
main
方法中创建Scanner
(或者在readBoolean2D 之前调用的新方法)无论如何)。您当前正在调用
readBoolean2D
作为程序的第一个操作,尝试在创建任何Scanner
之前读取不存在的变量。请注意,这一切与 try 块本身没有任何关系 - 事实上,变量是在单独的方法中声明的,这就是问题所在(以及方法调用的时间),不是
try
块。我会尝试将程序重组为:
我怀疑您实际上可以仅使用局部变量和参数 - 第一个方法将返回 一个
扫描仪
;第二种方法将使用扫描仪并返回它读取的数据;第三种方法将获取数据。You've only declared the
scan
variable within theflow
method. If you want to use that value within the other methods, you'll need to take one of two courses of action:As all your methods are static, for the first option you'd need to declare it as:
However, currently the
flow
method seems to do two radically different things:Scanner
(but doesn't actually use it)It looks to me like you should be creating the
Scanner
in yourmain
method (or in a new method which is called beforereadBoolean2D
, anyway). You're currently callingreadBoolean2D
as the very first action of the program, trying to read from a variable which doesn't exist, before anyScanner
has even been created.Note that none of this has anything to do with the
try
block itself - it's the fact that the variable is declared in a separate method which is the problem (and the timing of the method calls), not thetry
block.I would try to think of restructuring the program as:
I suspect you can actually get away with just local variables and parameters - the first method would return a
Scanner
; the second method would take a Scanner and return the data it reads; the third method would take the data.scan
变量必须是全局的、静态的,因为您的方法是静态的。如果您想从另一个方法甚至另一个类访问它,则必须全局声明它。The
scan
variable has to be global, and static, since your methods are static. If you want to access it from another method, or even another class, it will have to be declared globally.希望有帮助
Hope that helps
其他答案建议创建静态或全局 - 忽略它们。
您正在一个不使用它的函数中创建扫描仪扫描,不要在那里创建它,而是在需要的地方创建它。
readBoolean2D 和 readBoolean 都需要将扫描仪作为参数:
main 是您需要创建和使用扫描仪的地方:
The other answers suggest creating a static or global - ignore them.
You're creating the scanner scan in a function that does not use it, don't create it there, create it where it's needed.
readBoolean2D and readBoolean each need to take the scanner as an argument:
main is where you need to create and use the scanner: