1

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
5
  • 2
    Are both classes in the same file? I don't think you can declare them both public. Commented Sep 26, 2013 at 4:49
  • but error shouldn't be about public class has to be declared in a file name MySaxParser.java??? I don't understand this explain please Commented Sep 26, 2013 at 4:52
  • You're right Aayush, I get this error when I tried it: The public type MySaxParser must be defined in its own file, and not <identifier> expected. Nik can you provide more info? Commented Sep 26, 2013 at 4:55
  • 1
    Are you using Java5+ syntax? Otherwise it won't like the type annotation. Commented Sep 26, 2013 at 4:57
  • @Thilo Im using "1.4.1_01". is that an issue ? Commented Sep 26, 2013 at 5:32

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ah looks like you won't be able to use generics, unless you upgrade your runtime. See: stackoverflow.com/questions/4146197/does-java-1-4-have-generics
I see.. Im running it in a java 1.7 env and this error isnt there. I do see an IO exception thrown which Im debugging
2
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.

1 Comment

Thanks, resolved this with a 'modern' version! However I do see an IO exception being thrown that I am debugging. Do you know what could be the issue? The file registers.xml is in same directory.
0

It may be compilation error. So, you need to write these two class within same package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.