java 找不到标志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4235724/
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
Cannot Find Symbol
提问by helpMe123
I tried compiling my personal contacts database program, but I have nothing but "cannot find symbol" errors Should I be instantiating an object more often than once? If not, what is wrong with this code?
我尝试编译我的个人联系人数据库程序,但我只有“找不到符号”错误 我应该多次实例化一个对象吗?如果没有,这段代码有什么问题?
I moved writetoFile and readFile initializations to outside try block, and declared them inside, and it helped! Now the only compiler error messages are the ones shown at the very bottom.
我将 writetoFile 和 readFile 初始化移动到外部 try 块,并在内部声明它们,它有帮助!现在唯一的编译器错误消息是显示在最底部的那些。
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.Double;
import java.util.Arrays;
public class Contacts
{
String name;
String phone;
String addy;
String[] namesDB = new String[100];
int index=0;
public Contacts (String fullName, String phoneNo, String address)
{
name=fullName;
phone=phoneNo;
addy=address;
}
public String printName(String fullName)
{
for (index=0;index<100;index++)
{
if (fullName==namesDB[index])
System.out.println(namesDB[index]);
else
index++;
}
return namesDB[index];
}
public String deleteName(String fullName)
{
for (index=0;index<100;index++)
{
if (fullName==namesDB[index])
{
namesDB[index]= "0";
System.out.println("Record Deleted.");
}
else
index++;
}
return namesDB[index];
}
static String stripQuo(String str)
{
if (str.startsWith("\""))
{
str = str.substring(1, str.length());
}
if (str.endsWith("\""))
{
str = str.substring(0, str.length() - 1);
}
return str;
}
public static void main(String[] args)
{
String EMPTY = "Empty";
int index=0;
String[] namesDB = new String[100];
String fullName, phoneNo, address;
for (int i=0; i<100; i++)
{
Contacts w = new Contacts("-1", "-1", "-1");
w.namesDB[i]=EMPTY;
i++;
}
PrintWriter writetoFile=null;
BufferedReader readFile=null;
//Creating Writable File, PCD=Personal Contacts Database
try
{
writetoFile=new PrintWriter(new FileOutputStream("PCDFile.txt"));
readFile = new BufferedReader(new FileReader("PCDFile.txt"));
}
catch(FileNotFoundException ex)
{
System.out.println("Error opening PCDFile.txt");
System.exit(0);
}
catch(IOException gh)
{
System.out.println("Error reading from PCDFile.txt.");
System.exit(0);
}
//ADDING
if (args.length == 1)
{
String option=args[0];
if (option.equals("-a"))
{
for (int i=0;i<100; i++)
{
if(namesDB[i]==EMPTY)
{
System.out.println("Full Name: ");
Scanner keyboard = new Scanner(System.in);
fullName=keyboard.nextLine();
System.out.println("Phone: ");
phoneNo=keyboard.nextLine();
System.out.println("Address: ");
address=keyboard.nextLine();
Contacts w = new Contacts(fullName, phoneNo, address);
namesDB[i]="fullName/n" + "phoneNo/n" + "address/n";
writetoFile.println(namesDB[i]);
System.out.println(namesDB[i]);
}
else
index++;
}
}
//SEARCHING
else
{
option=stripQuo(option);
option=fullName;
String line= readFile.readLine();
for (index=0;index<100;index++)
{
if (line!=null)
{
if(line.equals(fullName))
{
Contacts w = new Contacts(fullName, phoneNo, address);
w.printName(fullName);
}
}
else
index++;
if (index==namesDB.length-1)
{
System.out.println("No record found for " + fullName + ".");
}
}
}
if (args.length == 2)
{
option=args[0];
fullName=args[1];
if (option.equals("-s"))
{
Contacts w = new Contacts(fullName, phoneNo, address);
fullName=w.stripQuo(fullName);
String line=readFile.readline();
for (index=0;index<100; index++)
{
if (line!=null)
{
if(line.equals(w.Contacts(fullName, phoneNo, address)))
{
w.printName(fullName);
}
}
else
index++;
if (index==namesDB.length-1)
{
System.out.println("No record found for " + fullName + ".");
}
}
}
else if (option.equals("-d"))
{
fullName=w.stripQuo(fullName);
String line=readFile.readline();
for (index=0;index<100; index++)
{
if (line!=null)
{
if(line.equals(w.Contacts(fullName, phoneNo, address)))
{
w.deleteName(fullName);
}
}
else if (index==namesDB.length-1)
System.out.println("No record found for " + fullName + ".");
else
index++;
}}}
writetoFile.close();
readFile.close();
}}}
Compiler Messages:
编译器消息:
/Contacts.java:170: cannot find symbol
symbol : method readline()
location: class java.io.BufferedReader
String line=readFile.readline();
^
/Contacts.java:176: cannot find symbol
symbol : method Contacts(java.lang.String,java.lang.String,java.lang.String)
location: class Contacts
if(line.equals(w.Contacts(fullName, phoneNo, address)))
^
/Contacts.java:191: cannot find symbol
symbol : variable w
location: class Contacts
fullName=w.stripQuo(fullName);
^
/Contacts.java:192: cannot find symbol
symbol : method readline()
location: class java.io.BufferedReader
String line=readFile.readline();
^
/Contacts.java:197: cannot find symbol
symbol : variable w
location: class Contacts
if(line.equals(w.Contacts(fullName, phoneNo, address)))
^
/Contacts.java:199: cannot find symbol
symbol : variable w
location: class Contacts
w.deleteName(fullName);
^
回答by user307666
A few style tips:
一些风格提示:
In general, avoid using if/else/loops without brackets, even if they only contain a single line of code. Especially you have if's with brackets and their corresponding else's without. This just makes it confusing for me to read.
一般来说,避免使用没有括号的 if/else/loops,即使它们只包含一行代码。特别是你有 if 带括号而它们对应的 else 没有。这只会让我读起来很困惑。
A database (DB) is like a kind of data structure. Naming an array variable with DB on the end is like having a variable "Set namesArrayList". It just says the variable refers to something which it doesn't. An array is not a database. Now potentially you may want to change the data structure you're using so you may just want to call it, 'names' to indicate it's a collection of things without specifying data structure, etc.
数据库 (DB) 就像一种数据结构。最后用 DB 命名数组变量就像有一个变量“Set namesArrayList”。它只是说变量指的是它不指的东西。数组不是数据库。现在,您可能想要更改正在使用的数据结构,因此您可能只想调用它,“名称”表示它是一组事物而不指定数据结构等。
Try not to write to much continuous unfactored code. Code that goes.
尽量不要写太多连续的未分解代码。去的代码。
if (args.length == 1) {
.. some huge slab here
} else if (args.length == 2) {
.. another huge slab here
}
if hard to read/keep track of.
如果难以阅读/跟踪。
It's probably better to take those huge slabs of text and put them into functions
将那些巨大的文本块放入函数中可能会更好
if (args.length == 1) {
whatWeDoFor1Arg(..params here..);
} else if (args.length == 2) {
whatWeDoFor2Args(..params here..);
}
Or even:
甚至:
if (args.length == 1) {
option=args[0];
if (option .equals(...) ) {
add(...) // or whatever
} else if (option.equals(...) ) {
search(..) // or whatever
}
} else if (args.length == 2) {
... etc ...
}
This makes your code more readable. Also the function names act like automatic comments for your subsections (ie. what's in the functions). Also since you redo alot of the same code, you may be able to just do this in a function, then call the function multiple times (with different parameters), ie. reuse it.
这使您的代码更具可读性。此外,函数名称就像您的小节的自动注释(即函数中的内容)。此外,由于您重做了很多相同的代码,因此您可以在一个函数中执行此操作,然后多次调用该函数(使用不同的参数),即。重用它。
Also, what are you actually trying to do with the Contacts class. Why not have a class to deal with individual contacts, eg: a Contact class, with say, name, address, phoneNum, fields. And a class to deal with a collection of contacts, eg: AddressBook class. Which contains a field which is something along the lines of: ArrayList<Contact> contacts , ie. an collection of contacts, and some methods to operate of this, eg: add, search, delete etc. You don't have to store a string representation of each object in your collection, you can store the actual objects themselves.
另外,您实际上想用 Contacts 类做什么。为什么没有一个类来处理个人联系人,例如:一个 Contact 类,包括姓名、地址、电话号码、字段。还有一个类来处理联系人的集合,例如:AddressBook 类。其中包含一个类似于以下内容的字段: ArrayList<Contact> contacts ,即。联系人集合,以及一些操作方法,例如:添加、搜索、删除等。您不必在集合中存储每个对象的字符串表示,您可以存储实际对象本身。
Doing this would make the code to run the command line very simple.
这样做会使运行命令行的代码变得非常简单。
Now regarding the errors:
现在关于错误:
First error:
第一个错误:
Contacts.java:170: cannot find symbol symbol : method readline()
location: class java.io.BufferedReader String line=readFile.readline();
You've mistyped the name of the function. It should be readLine() with a capital L.
您输入了错误的函数名称。它应该是带有大写 L 的 readLine()。
Second error:
第二个错误:
^ /Contacts.java:176: cannot find symbol symbol : method
Contacts(java.lang.String,java.lang.String,java.lang.String)
location: class Contacts if(line.equals(w.Contacts(fullName, phoneNo, address)))
What are you actually trying to do here. There's no Contacts method on your contacts class. Seems pretty self-explanatory. Going w.Contacts() where w is a Contacts object, tries to invoke a method called Contacts of the object (which obviously doesn't exist). Now if you define a toString() method for Contacts which returns a String of the same form as what's in your list of names (ie. namesDB). Then you can go: if ( line.equals(w.toString() ) ...
你到底想在这里做什么。您的联系人类中没有 Contacts 方法。似乎不言自明。转到 w.Contacts() ,其中 w 是 Contacts 对象,尝试调用对象的 Contacts 方法(显然不存在)。现在,如果您为 Contacts 定义一个 toString() 方法,该方法返回一个与您的姓名列表(即namesDB)中的内容形式相同的字符串。然后你可以去: if ( line.equals(w.toString() ) ...
Third error:
第三个错误:
^ /Contacts.java:191: cannot find symbol symbol : variable w
location: class Contacts fullName=w.stripQuo(fullName);
You haven't actually declared a Contacts variable called 'w' either in this else statement or anywhere "above" this else statement. ie. you haven't declared a 'w' variable within the "scope" of this line.
您实际上并未在此 else 语句中或此 else 语句“上方”的任何位置声明名为“w”的 Contacts 变量。IE。您尚未在此行的“范围”内声明 'w' 变量。
So you could go: Contacts w = new Contacts(fullName, phoneNo, address); at the top of the else statement or in the if statement outside it.
所以你可以去: Contacts w = new Contacts(fullName, phoneNo, address); 在 else 语句的顶部或其外部的 if 语句中。
BUT, this is dodgy because you're making a Contacts object with a fullName you've got from a commandline arg (this part is fine) and some uninitialised variables (address/phoneNo) decalred at the top of your main function!!!
但是,这很狡猾,因为您正在使用从命令行 arg 获得的 fullName 创建一个 Contacts 对象(这部分很好)和一些未初始化的变量(address/phoneNo)贴在主函数的顶部!!!
Actually, since this is a static method, you should really be going: Contacts.stripQuo()
实际上,由于这是一个静态方法,你真的应该去: Contacts.stripQuo()
You should call static methods using the name of the class, not the name of an object of that class (even though that might work). (see above for how).
您应该使用类的名称而不是该类的对象的名称来调用静态方法(即使这可能有效)。(参见上文了解如何)。
Fourth error:
第四个错误:
same as first error
与第一个错误相同
Fifth error:
第五个错误:
Same thing as both the second error and the third error.
与第二个错误和第三个错误相同。
Sixth error:
错误六:
Same as third error. Except you actually do need to call this on a object (ie. because it's not a static method)
与第三个错误相同。除非您确实需要在对象上调用它(即,因为它不是静态方法)
回答by ajwood
Your writeToFile object only exists within the scope of the try statement in which it was created. Declare it outside of the try block, but initialize it inside.
您的 writeToFile 对象仅存在于创建它的 try 语句的范围内。在 try 块外部声明它,但在内部初始化它。
The same goes for your readFile object. And the w object, only it's in an if block. You should have a look at variable scoping: Language/VariableScope.htm">http://www.java2s.com/Tutorial/Java/0020_Language/VariableScope.htm
您的 readFile 对象也是如此。而 w 对象,只有它在一个 if 块中。你应该看看变量范围:Language/VariableScope.htm">http://www.java2s.com/Tutorial/Java/0020_Language/VariableScope.htm
回答by Supuhstar
Fixes
修复
Well, first of all, Contacts w = new Contacts(fullName, phoneNo, address);
must be a couple lines up, so it is outside the if braces, making it visible in the following else if statement.
好吧,首先,Contacts w = new Contacts(fullName, phoneNo, address);
必须是一对夫妇的行,所以它在 if 大括号之外,使其在下面的 else if 语句中可见。
Next, the occurences of .readline()
should be .readLine()
(capital "L").
接下来,出现的.readline()
应该是.readLine()
(大写“L”)。
Third, the part that says if (line.equals(w.Contacts(fullName, phoneNo, address)))
cannot be done. You can not call the constructor of a class via the object. I think what you want is if (line.equals(new Contacts(fullName, phoneNo, address)))
, but this is still improper. You are trying to compare a String
(line
) object and a Contacts
object. I don't know what you are trying to accomplish, but you should compare two String
s or two Contacts
s, here. Perhaps try line.equals(w.name)
if that is what you are trying to do.
第三,说的if (line.equals(w.Contacts(fullName, phoneNo, address)))
不能做的部分。您不能通过对象调用类的构造函数。我想你想要的是if (line.equals(new Contacts(fullName, phoneNo, address)))
,但这仍然不合适。您正在尝试比较String
( line
) 对象和Contacts
对象。我不知道你想要完成什么,但你应该在这里比较两个String
或两个Contacts
。也许尝试一下,line.equals(w.name)
如果这就是你想要做的。
Fourth, whenever you get an error from readFile.readLine();
, it is because of an uncaught IOException
. I would fix this with a try
-catch
block, but a throws
modifier at the top of the method will work, too.
第四,每当您从 中得到错误时readFile.readLine();
,都是由于未捕获的IOException
。我会用try
-catch
块解决这个问题,但throws
方法顶部的修饰符也可以使用。
Fifth, where you have String fullName, phoneNo, address;
, you should have, instead, String fullName = "", phoneNo = "", address = "";
. In this way, you will not rin into any compiler errors about not having initialized them.
第五,在你有的地方String fullName, phoneNo, address;
,你应该有,而不是String fullName = "", phoneNo = "", address = "";
。这样,您就不会因为没有初始化它们而陷入任何编译器错误。
Sixth, on one line in your printName
, deleteName
, and main
method, you say fullName == namesDB[index]
, which will give you runtime errors, as you are comparing two non-primitive objects with ==
. Replace this with fullName.equals(namesDB[index])
第六,在您的printName
,deleteName
和main
方法的一行中,您说fullName == namesDB[index]
,当您将两个非原始对象与==
. 将其替换为fullName.equals(namesDB[index])
Also, you imported java.lang.Double
. Why? First off, all java.lang
files and packages are automatically imported. Second, you shouldn't use Double
s, but instead double
s for math. Also, you imported java.util.Arrays
, but never use it.
此外,您导入了java.lang.Double
. 为什么?首先,所有java.lang
文件和包都会自动导入。其次,你不应该使用Double
s,而应该使用s 来double
表示数学。此外,您导入了java.util.Arrays
,但从未使用过它。
My Solution
我的解决方案
Below is my solution to your problems. Since I don't know what you are trying to accomplish, you may have to tweak it a bit.
以下是我对您的问题的解决方案。由于我不知道您要完成什么,因此您可能需要稍微调整一下。
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Contacts
{
String name;
String phone;
String addy;
String[] namesDB = new String[100];
int index = 0;
public Contacts(String fullName, String phoneNo, String address)
{
name = fullName;
phone = phoneNo;
addy = address;
}
public String printName(String fullName)
{
for (index = 0; index < 100; index++)
{
if (fullName.equals(namesDB[index]))
{
System.out.println(namesDB[index]);
}
else
{
index++;
}
}
return namesDB[index];
}
public String deleteName(String fullName)
{
for (index = 0; index < 100; index++)
{
if (fullName.equals(namesDB[index]))
{
namesDB[index] = "0";
System.out.println("Record Deleted.");
}
else
{
index++;
}
}
return namesDB[index];
}
static String stripQuo(String str)
{
if (str.startsWith("\""))
{
str = str.substring(1, str.length());
}
if (str.endsWith("\""))
{
str = str.substring(0, str.length() - 1);
}
return str;
}
public static void main(String[] args)
{
String EMPTY = "Empty";
int index = 0;
String[] namesDB = new String[100];
String fullName = "", phoneNo = "", address = "";
for (int i = 0; i < 100; i++)
{
Contacts w = new Contacts("-1", "-1", "-1");
w.namesDB[i] = EMPTY;
i++;
}
PrintWriter writetoFile = null;
BufferedReader readFile = null;
//Creating Writable File, PCD=Personal Contacts Database
try
{
writetoFile = new PrintWriter(new FileOutputStream("PCDFile.txt"));
readFile = new BufferedReader(new FileReader("PCDFile.txt"));
}
catch (FileNotFoundException ex)
{
System.out.println("Error opening PCDFile.txt");
System.exit(0);
}
catch (IOException gh)
{
System.out.println("Error reading from PCDFile.txt.");
System.exit(0);
}
//ADDING
if (args.length == 1)
{
String option = args[0];
if (option.equals("-a"))
{
for (int i = 0; i < 100; i++)
{
if (namesDB[i].equals(EMPTY))
{
System.out.println("Full Name: ");
Scanner keyboard = new Scanner(System.in);
fullName = keyboard.nextLine();
System.out.println("Phone: ");
phoneNo = keyboard.nextLine();
System.out.println("Address: ");
address = keyboard.nextLine();
Contacts w = new Contacts(fullName, phoneNo, address);
namesDB[i] = "fullName/n" + "phoneNo/n" + "address/n";
writetoFile.println(namesDB[i]);
System.out.println(namesDB[i]);
}
else
{
index++;
}
}
}
//SEARCHING
else
{
option = stripQuo(option);
option = fullName;
String line = "";
try
{
line = readFile.readLine();
}
catch (IOException ex)
{
System.err.println("Could not read line: " + ex);
}
for (index = 0; index < 100; index++)
{
if (line != null)
{
if (line.equals(fullName))
{
Contacts w = new Contacts(fullName, phoneNo, address);
w.printName(fullName);
}
}
else
{
index++;
}
if (index == namesDB.length - 1)
{
System.out.println("No record found for " + fullName + ".");
}
}
}
if (args.length == 2)
{
option = args[0];
fullName = args[1];
Contacts w = new Contacts(fullName, phoneNo, address);
if (option.equals("-s"))
{
fullName = w.stripQuo(fullName);
String line = "";
try
{
line = readFile.readLine();
}
catch (IOException ex)
{
Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
}
for (index = 0; index < 100; index++)
{
if (line != null)
{
if (line.equals(w.name))
{
w.printName(fullName);
}
}
else
{
index++;
}
if (index == namesDB.length - 1)
{
System.out.println("No record found for " + fullName + ".");
}
}
}
else if (option.equals("-d"))
{
fullName = w.stripQuo(fullName);
String line = "";
try
{
line = readFile.readLine();
}
catch (IOException ex)
{
Logger.getLogger(Contacts.class.getName()).log(Level.SEVERE, null, ex);
}
for (index = 0; index < 100; index++)
{
if (line != null)
{
if (line.equals(w.name))
{
w.deleteName(fullName);
}
}
else if (index == namesDB.length - 1)
{
System.out.println("No record found for " + fullName + ".");
}
else
{
index++;
}
}
}
}
writetoFile.close();
try
{
readFile.close();
}
catch (IOException ex)
{
System.err.println("Could not close file: " + ex);
}
}
}
}
A Question to You
给你一个问题
As a note, most of this could have been avoided if you were using NetBeans. What IDE do you use?
需要注意的是,如果您使用 NetBeans,大部分情况都可以避免。你用什么IDE?
回答by Costis Aivalis
Straighten your scoping like user512652 said, and then compare Strings with .equals() instead of ==
像 user512652 所说的那样拉直您的范围,然后将字符串与 .equals() 而不是 == 进行比较