Cannot Find Symbol
我试图编译我的个人联系人数据库程序,但我什么都没有但"找不到符号"错误我应该多次实例化一个对象吗?如果没有,这个代码有什么问题?
我将writetofile和readfile初始化移到了外部的try块,并在内部声明了它们,这很有帮助!现在,唯一的编译器错误消息显示在最底部。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 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(); }}} |
编译器消息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /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); ^ |
一些贴士:风格P></
在通用,使用if / else/环avoid甚至if they only without brackets,contain单打在线of队列。如果你有一个S especially brackets和他们与别人的不对应。它只是让我confusing for this to read。P></
数据库(DB)is like a kind of数据结构。阵列变量与数据库在线安naming the end is like a variable"有namesarraylist集"。它只是说something to which the variable"是指不安恩。is not a数据库阵列。现在你可能想potentially change the数据结构你知道你只是想使用它可能是呼叫,名称是指定的collection of things without是specifying数据结构等。P></
not to write to多尝试未经修正的连续队列。我去排队。P></
1 2 3 4 5 | if (args.length == 1) { .. some huge slab here } else if (args.length == 2) { .. another huge slab here } |
如果硬盘的读/ keep track of。P></
更好的是把那些巨大的板坯probably to them into of text函数把P></
1 2 3 4 5 | if (args.length == 1) { whatWeDoFor1Arg(..params here..); } else if (args.length == 2) { whatWeDoFor2Args(..params here..); } |
或是:P></
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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 ... } |
这使你的代码更可读。also the function for your names like自动对法(subsections IE功能。什么是茶)。因为你我也alot of the same to You may be队列,这只是我在能在那么多的函数呼叫(the function with different parameters)时代,IE。重用它。P></
我也知道,你是想给contacts with the class。not to have a class为什么交易个体交往与接触,如:class name和address phonenum说,,,,等领域。和协议与collection of class to contacts,EG:中舱。which is something which contains a field of the Lines在<接触>:ArrayList contacts(collection of contacts。安布尔操作方法,to some of this,EG:add,搜索、删除等。*商店string representation of have to each collection对象在你的商店,你可以在实际的对象themselves。P></
我做这行代码运行make the the线很简单。P></
现在对于"错误:P></
第一误差:P></
1 2 | Contacts.java:170: cannot find symbol symbol : method readline() location: class java.io.BufferedReader String line=readFile.readline(); |
你键入的the name of the function。EN should be readline()与资本。P></
第二误差:P></
1 2 3 |
我想你知道我在这里。没有你的联系人联系人在线类的方法。自注释33看起来漂亮。去(在)w.contacts contacts对象W是在tries method called to,of the invoke contacts对象(which exist不明显)。现在,如果你在定义toString()方法返回a串contacts for which of the same as是什么在你的form of names(IE列表。namesdb)。然后你可以去line.equals:if((()w.tostring)……P></
错误三:P></
1 2 | ^ /Contacts.java:191: cannot find symbol symbol : variable w location: class Contacts fullName=w.stripQuo(fullName); |
你真的没在我declared contacts变量W是在表或者其他"above this or else this statement"*。谢谢。你没在declared变量W是在网上"扫帚"of this。P></
我知道你可以去:W =新的联系人(姓名,地址phoneno contacts,);else statement of the at the Top or if the statement恩在外面。P></
但这是可疑的,因为你使你的姓名与交往对象要从命令行参数(this is some结束部分(address)和uninitialised元/ phoneno)decalred at the main function of顶你!!!!!!!!!!!!!!!P></
其实,这是因为在静电法,你真的想:contacts.stripquo(should)beP></
You should call方法using the name of the静态类,not the name of an object class of that(即使that might work)。(see how for above)。P></
错误四:P></
same as第一误差P></
第五:误差P></
same thing as both the second三误差和误差。P></
第六:误差P></
same as三误差。你知道除了我need to this在线客服(在IE对象。因为这不是静电法)P></
WriteToFile对象只存在于创建它的Try语句的范围内。在try块之外声明它,但在内部初始化它。
readfile对象也是如此。而w对象,只在if块中。您应该看看变量范围:language/variablescope.htm">http://www.java2s.com/tutorial/java/0020_language/variablescope.htm(语言/variablescope.htm)
修正
首先,
接下来,
第三,说江户十一〔三〕的那部分不能做。不能通过对象调用类的构造函数。我想你想要的是
第四,每当你从
第五,如果你有
第六,在您的
此外,您还进口了
下面是我解决你问题的方法。因为我不知道你想完成什么,你可能需要稍微调整一下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | 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); } } } } |
问你一个问题
作为说明,如果你使用NETBeBes,大部分都是可以避免的。你使用什么IDE?
像user512652所说的那样理顺范围,然后将字符串与.equals()进行比较,而不是==