Getting "error: cannot find symbol"
Getting "error: cannot find symbol"
我收到消息
" Example.java:21: error: cannot find symbol
Scanner finput = new Scanner(filed);
^
symbol: variable filed
location: class Example "
当我编译我的程序时。我已尝试查找此内容,但无法弄清楚为什么会出现此错误。
这是代码
public class Example
{
public static void main(String[] args) throws Exception
{
java.io.File filer = new java.io.File("RESULT.txt");
System.out.println("Choose the location of the data file");
JFileChooser fileCh = new JFileChooser();
if (fileCh.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
java.io.File filed = fileCh.getSelectedFile();
}
else {
System.out.println ("No file choosen.");
System.exit(1);
}
Scanner finput = new Scanner(filed);
}
}
您收到此错误是因为正在从非法范围访问 filed
。尝试在 if 语句之外定义 java.io.File filed
。或者,也许您想使用 Scanner finput = new Scanner(filer)
?
如前所述,您需要将 filed
变量的声明移到 if 语句之外。您的代码可以是这样的:
public static void main(String[] args) throws Exception {
java.io.File filer = new java.io.File("RESULT.txt");
System.out.println("Choose the location of the data file");
JFileChooser fileCh = new JFileChooser();
java.io.File filed = null;
if (fileCh.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
filed = fileCh.getSelectedFile();
} else {
System.out.println("No file choosen.");
System.exit(1);
}
Scanner finput = new Scanner(filed);
}
现在可以正常使用了。
我收到消息
" Example.java:21: error: cannot find symbol
Scanner finput = new Scanner(filed); ^
symbol: variable filed
location: class Example "
当我编译我的程序时。我已尝试查找此内容,但无法弄清楚为什么会出现此错误。
这是代码
public class Example
{
public static void main(String[] args) throws Exception
{
java.io.File filer = new java.io.File("RESULT.txt");
System.out.println("Choose the location of the data file");
JFileChooser fileCh = new JFileChooser();
if (fileCh.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
java.io.File filed = fileCh.getSelectedFile();
}
else {
System.out.println ("No file choosen.");
System.exit(1);
}
Scanner finput = new Scanner(filed);
}
}
您收到此错误是因为正在从非法范围访问 filed
。尝试在 if 语句之外定义 java.io.File filed
。或者,也许您想使用 Scanner finput = new Scanner(filer)
?
如前所述,您需要将 filed
变量的声明移到 if 语句之外。您的代码可以是这样的:
public static void main(String[] args) throws Exception {
java.io.File filer = new java.io.File("RESULT.txt");
System.out.println("Choose the location of the data file");
JFileChooser fileCh = new JFileChooser();
java.io.File filed = null;
if (fileCh.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
filed = fileCh.getSelectedFile();
} else {
System.out.println("No file choosen.");
System.exit(1);
}
Scanner finput = new Scanner(filed);
}
现在可以正常使用了。