Can someone help me resolve this innocuous Identifier expected error? I am declaring a Register class and trying to make a list of Register objects.
UPDATE : 'Register' is locally scoped in MySaxParser.java as Meesh suggested. But I still see identifier expected error. The complete code and error message is below:
The issue was seen using java version "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;}
}
ERROR:
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
java version "1.4.1_01"
List<Register> registerL;
That is a really old Java version. Generics have been introduced in Java5, so in order to use "modern" Java, you need to update to a newer version.
If both Register
and MySaxParser
are public classes (meaning that they can be used by other classes), they both need to be in a file of their own. If not, you can make Register
locally scoped, and access it only in MySaxParser
. For example (details omitted for brevity):
public class MySaxParser {
List<Register> registers;
}
class Register {
String value;
}
Also, see @Thilo's comment above. You may need to change your use of generics depending on which JRE/JDK you're using.
It may be compilation error. So, you need to write these two class within same package.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.