java编译错误:找不到符号。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20186627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile error in java :cannot find symbol.
提问by user2977165
I have this code that is taking a text file and turning it into a string and then separating parts of the string into different elements of an arraylist.
我有这段代码,它正在获取一个文本文件并将其转换为一个字符串,然后将字符串的一部分分成一个数组列表的不同元素。
import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;
public class Grocery{
public Grocery(){
File inFile = new File ("lists.txt");
Scanner input = new Scanner (inFile);
String grocery;
{
grocery = input.nextLine();
}
}
public void makeSmallerLists(){
String listLine;
String line;
ArrayList<String> smallList = new ArrayList<String>();
while(input.hasNextLine()){
line = input.nextLine;
if(line.equals("<END>")){
smallList.add(listLine);
} else{
listLine = listLine + "\n" + line;
}
}
}
}
However when I try to compile this it gives me two errors:
但是,当我尝试编译它时,它给了我两个错误:
javac Message.java Message.java:31: cannot find symbol symbol : variable input location: class Message while(input.hasNextLine()){ ^ Message.java:32: cannot find symbol symbol : variable input location: class Message line = input.nextLine; ^
javac Message.java Message.java:31: 找不到符号符号: 变量输入位置: class Message while(input.hasNextLine()){ ^ Message.java:32: 找不到符号符号: 变量输入位置: class Message line = input.nextLine; ^
How do I fix this? I really don't know what's wrong.
我该如何解决?我真的不知道怎么了。
I fixed that and now my error says $ javac Message.java Message.java:34: cannot find symbol symbol : variable nextLine location: class java.util.Scanner line = input.nextLine; ^
我解决了这个问题,现在我的错误是 $ javac Message.java Message.java:34: cannot find symbol symbol : variable nextLine location: class java.util.Scanner line = input.nextLine; ^
^
Now what is wrong?
现在出了什么问题?
采纳答案by Suresh Atta
Scanner input = new Scanner (inFile);
input
is local to the constructor, you cannot access outside of it, and you are trying to access in makeSmallerLists()
method. Make it as a instance member, So that it available through out the class
other than static
context.
input
对构造函数来说是本地的,你不能在它之外访问,并且你试图在makeSmallerLists()
方法中访问 。将其作为实例成员,以便它在上下文之外的class
其他内容中可用static
。
public class Grocery{
Scanner input;
and in constructor
并在构造函数中
public Grocery(){
File inFile = new File ("lists.txt");
input = new Scanner (inFile);
回答by SudoRahul
That is because the Scanner object input
has been declared inside your constructor(local scope of the constructor) and thus its not visible in your makeSmallerLists()
. You need to declare it as an instance variable so that it would be accessible in all the methods of the class.
那是因为 Scanner 对象input
已在您的构造函数(构造函数的本地范围)中声明,因此它在您的makeSmallerLists()
. 您需要将其声明为实例变量,以便在类的所有方法中都可以访问它。
public class Grocery {
Scanner input; // declared here, as an instance variable
public Grocery(){
File inFile = new File ("lists.txt");
input = new Scanner (inFile); // initialized here
...
}
...
public void makeSmallerLists() {
...
while(input.hasNextLine()) { // accessible everywhere within the class
...
}
}
回答by Masudul
You have variable scope problem. You can't get access a field outside of scope. Declare Scanner as globally, outside of costructor.
您有可变范围问题。您无法访问范围之外的字段。在 costructor 之外将 Scanner 声明为全局。
public class Grocery{
Scanner input = null;// Declare Scanner here.
public Grocery(){
.....
input=new Scanner (inFile);
}
Also append method bracket ()
.
还附加方法括号()
。
public void makeSmallerLists(){
......
while(input.hasNextLine()){
line = input.nextLine();// Append () after method.
.....
}
回答by Maroun
One solution is to have a class memberof type Scanner
:
一种解决方案是拥有一个类型为的类成员Scanner
:
private Scanner input;
And in the constructor, construct it:
在构造函数中,构造它:
public class Grocery {
private Scanner input;
public Grocery() {
...
...
input = new Scanner (inFile);
}
...
}
Now input
is not limited to the scopeof the constructor, it'll be accessible through the whole class.
现在input
不限于构造函数的范围,它可以通过整个类访问。
Consider this example:
考虑这个例子:
public class Example {
int n = 0; //n known everywhere in the class
if(n == 0) {
int n1 = 1;
if(n1 == 1) {
int n2 = 2;
//n1 is known here
}
//n2 is not known here
}
//n1 is not known here
}
回答by Vinayak Pahalwan
input
is not accesible outside the constructor...it's declared insisde the constructor
input
在构造函数之外不可访问……它在构造函数内部声明