有人可以帮我解决这个无害的标识符预期错误吗?我正在声明一个 Register 类并尝试制作一个 Register 对象列表。
更新:正如 Meesh 所建议的,“注册”在 MySaxParser.java 中是本地范围的。但我仍然看到标识符预期错误。完整的代码和错误信息如下:
使用 java 版本“1.4.1_01”发现了该问题
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MySaxParser extends DefaultHandler {
List<Register> registerL;
String xmlFileName;
String tmpValue;
Register registerTmp;
public MySaxParser(String xmlFileName) {
this.xmlFileName = xmlFileName;
registerL = new ArrayList<Register>();
parseDocument();
printDatas();
}
private void parseDocument() {
// parse
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
parser.parse(xmlFileName, this);
} catch (ParserConfigurationException e) {
System.out.println("ParserConfig error");
} catch (SAXException e) {
System.out.println("SAXException : xml not well formed");
} catch (IOException e) {
System.out.println("IO error");
}
}
private void printDatas() {
// System.out.println(bookL.size());
for (Register tmpReg : registerL) {
System.out.println(tmpReg.toString());
}
}
@Override
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
// if current element is book , create new book
// clear tmpValue on start of element
if (elementName.equalsIgnoreCase("reg")) {
registerTmp = new Register();
registerTmp.setregName(attributes.getValue("regname"));
registerTmp.setaddr(attributes.getValue("addr"));
}
}
@Override
public void characters(char[] ac, int i, int j) throws SAXException {
tmpValue = new String(ac, i, j);
}
public static void main(String[] args) {
new MySaxParser("register.xml");
}
}
/*****
* Model class for Regsiter
* ****/
class Register {
String regName;
String addr;
//Setters
public void setregName(String regName ) { this.regName = regName; }
public void setaddr (String addr ) { this.addr = addr; }
//Getters
public String getregName() { return this.regName; }
public String getaddr() { return this.addr;}
}
错误:
MySaxParser.java:19: <identifier> expected
List<Register> registerL;
^
MySaxParser.java:26: '(' or '[' expected
registerL = new ArrayList<Register>();
^
MySaxParser.java:46: ';' expected
for (Register tmpReg : registerL) {
^
MySaxParser.java:49: illegal start of expression
}
^
MySaxParser.java:48: ';' expected
}
^
MySaxParser.java:50: illegal character: \64
@Override
^
MySaxParser.java:62: illegal character: \64
@Override
^
MySaxParser.java:68: <identifier> expected
}
^
MySaxParser.java:26: cannot resolve symbol
symbol : variable registerL
location: class MySaxParser
registerL = new ArrayList<Register>();
^
9 errors