Java Netbeans 错误找不到符号,符号:类输出,位置:类系统,<标识符> 预期,类型的非法开头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13424703/
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
Java Netbeans Error Cannot find symbol, symbol: class out, location: class System, <identifier> expected, illegal start of type
提问by Grafica
I'm getting an error in Netbeans in my driver class on this line:
我在这一行的驱动程序类中的 Netbeans 中遇到错误:
System.out.print("Time waited is: " + (serializedTime - System.currentTimeMillis())/1000 + " secs.");
The error is: "Cannot find symbol, symbol: class out, location: class System, expected, illegal start of type.
错误是:“找不到符号,符号:类输出,位置:类系统,预期,类型的非法开始。
import domain.PersistentObject;
import domain.PopulationRecord;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import utilities.MiniProj2Utilities;
public class MiniProj2Driver {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Make sure that you develop your main() method with reduced code as shown below.
// Not the use of private static methods in the driver called from main() method.
// Read the CSV file records into a list of PopulationRecord objects...
List<PopulationRecord> popList = MiniProj2Utilities.getDataRecords();
// Display the list contents and size...
MiniProj2Utilities.displayRecordsFromList(popList);
// Create and populate the PersistentObject...
PersistentObject po = MiniProj2Utilities.getPersistentObject(popList);
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("./data/population-record.ser"));
oos.writeObject(po);
} catch (IOException ex) {
}
long serializedTime = System.currentTimeMillis();
System.out.println(po);
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
System.out.println("Sleep Error");
}
try {
ObjectInputStream oos = new ObjectInputStream(new FileInputStream("./data/population-record.ser"));
PersistentObject po1 = (PersistentObject)oos.readObject();
} catch (IOException ex)
{
} catch (ClassNotFoundException ex) {
}
}
System.out.print("Time waited is: " + (serializedTime - System.currentTimeMillis())/1000 + " secs.");
// Read the CSV file records into a list of PopulationRecord objects...
private static List<PopulationRecord> getDataRecords() {
BufferedReader br = null;
String line = null;
List<PopulationRecord> list = new ArrayList<PopulationRecord>();
try {
br = new BufferedReader(new FileReader("data/NST_EST2011_ALLDATA.csv"));
br.readLine(); // Remove header line from file...
while ((line = br.readLine()) != null) {
String[] tokens = line.split(",");
//System.out.println(line);
PopulationRecord pr = new PopulationRecord(
tokens[0], tokens[1], tokens[2], tokens[3], tokens[4],
Integer.parseInt(tokens[5]), Integer.parseInt(tokens[6]),
Long.parseLong(tokens[7]), Long.parseLong(tokens[8]),
Long.parseLong(tokens[9]), Long.parseLong(tokens[10]),
Long.parseLong(tokens[11]), Long.parseLong(tokens[12]),
Long.parseLong(tokens[13]), Long.parseLong(tokens[14]),
Long.parseLong(tokens[15]), Long.parseLong(tokens[16]),
Long.parseLong(tokens[17]), Long.parseLong(tokens[18]),
Long.parseLong(tokens[19]), Long.parseLong(tokens[20]),
Long.parseLong(tokens[21]), Long.parseLong(tokens[22]),
Long.parseLong(tokens[23]), Long.parseLong(tokens[24]),
Long.parseLong(tokens[25]), Long.parseLong(tokens[26]),
Long.parseLong(tokens[27]), Long.parseLong(tokens[28]),
Long.parseLong(tokens[29]));
list.add(pr);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(MiniProj2Driver.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MiniProj2Driver.class.getName()).log(Level.SEVERE, null, ex);
}
return list;
}
// Display the list contents and size...
private static void displayRecordsFromList(List<PopulationRecord> list) {
for (PopulationRecord record : list) {
System.out.println(record);
}
System.out.println("Population records processed: " + list.size() + list.get(9));
}
private static PersistentObject getPersistentObject(List<PopulationRecord> list) {
PersistentObject po = new PersistentObject();
po.setSerializedTime(new Date());
po.setPopulationList(list);
return po;
}
}
采纳答案by kosa
One issue is, your System.out
statement is outside method.
一个问题是,您的System.out
声明是方法之外的。
}
System.out.print("Time waited is: " + (serializedTime - System.currentTimeMillis())/1000 + " secs.");
move it to inside the braces
将其移动到大括号内
System.out.print("Time waited is: " + (serializedTime - System.currentTimeMillis())/1000 + " secs.");
}
回答by Azeemali Hashmani
public class potpie {
private int day = 0;
private int month = 0;
private int year = 0;
public potpie(int d, int m, int y){
day = d;
month = m;
year = y;
/*
No error of symbol: class out --------> SINCE IT IS INSIDE METHOD
*/
System.out.printf("This is today's date = %d %d %d", month, day, year);
}
/*
Cannot find symbol: class out --------> SINCE IT IS OUTSIDE METHOD
*/
System.out.printf("This is today's date = %d %d %d", month, day, year);
}
回答by Mrudav Shukla
The basic thing is Java works on Encapsulation i.e wrapping code and the data together, and thus the concept of having classes. Now to operate on the instance variables of the class, you can only do it by some method. Your l.o.c, i.e "System.out.print("your data");" is lying loosely in the body of the class and it has the instance variables of the class. Also as stated earlier, to operate on instance variables you can do so by using methods or constructors only and thus since this code is lying loosely and there is no method to invoke it, you get the error. So put it into the method as suggested by user Nambari above.
Java 的基本原理是封装,即将代码和数据包装在一起,因此有了类的概念。现在要对类的实例变量进行操作,只能通过某种方式进行。您的位置,即“System.out.print("your data");” 松散地位于类的主体中,并且具有类的实例变量。同样如前所述,要对实例变量进行操作,您只能通过使用方法或构造函数来执行此操作,因此由于此代码松散且没有调用它的方法,因此您会收到错误消息。所以把它放到上面用户 Nambari 建议的方法中。