1

I have a Java Class like this:

public class Employee {
  String Name;
  int Id;
  int Age;

  void setName(String tempVal) {
    Name = tempVal;
    System.out.println(Name);
    System.out.println("\n");
  }

  void setId(int parseInt) {
    Id = parseInt;
    System.out.println(Id);
    System.out.println("\n");
  }

  void setAge(int parseInt) {
    Age = parseInt;
    System.out.println(Age);
    System.out.println("\n");
  }

}

Now I want to parse a employees.xml file using SAXParser using the code in the link: http://totheriver.com/learn/xml/xmltutorial.html#5.2

The problem is when I am adding the tempEmp to the list and accessing the list to print its value in printData() method, the output is something like:

No of Employees '3'.

Employee@140de537

Employee@1c43882a

Employee@15a08be5

Now, how do I extract the name, age and id of the employee individually?

2

4 Answers 4

2

I guess you are adding the Employee object to the list and printing the objects directly from list.If you dont override the toString() method, it will call the toString() method of Object class(superclass of all class), which will be returing the classname@hashcode(hashcode of object).If you want to print some data from your class, you need to override toString() method in your class and return the format you require.

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

9 Comments

yaa rite i got you point.. but i have another doubt.. if i want to just access the value rather than printing it.. then what to do? as in say i want to assign a variable int agee= age of the employee, how would i do that?
Just like the set methods you have, you have to implement getters also. public String getName() { return Name; } public int getAge() { return Age; } public int getId() { return Id; } Then you can access any data like int age = yourEmployeeObject.getAge() Hope this helps..
i am not able to extract the age values from the get methods. i implemented get methods in the same manner u said. the i m doing: int age=myEmpls.get(0).getAge();, where myEmpls is the name of the list. can u please help?
can you show me the code?(where you are declaring your List and getter methods)Remember, the getter methods must be public
totheriver.com/learn/xml/code/SAXParserExample.java totheriver.com/learn/xml/code/Employee.java these are the codes... now in printdata method in SAX.java one, if i change the line it.next().toString() to it.next().getAge() it throws an error
|
2

You have to implement a toString() method in the Employee class for it to be displayed correctly, something along these lines:

@Override
public String toString() {
    return Id + " " + Name + " " + Age;
}

Also, remember that in Java attribute names should start with a lowercase character, not uppercase.

5 Comments

can u please tell me how to do that... i am new to java
and if i need to extract the data individually one by one... as in something like it.next().Name,it.next.Id etc.. i mean in general can u tell me what exactly is the format of the it.next() element??
Adding in this toString() method in your Employee class will solve your problem. It tells java to run this function every time you try to print an object of type Employee.
but what if i dont want to print... i just want to access the value and store it somewhere.. how to do it then?
Whatever you are using to learn Java, READ it. You need to know the basics of how to program before coming to SO.
2

You want to add some getter methods.

You should also check out the code conventions for Java - some of your variables start with an uppercase letter where they should not.

Comments

1

To get each value individually, you need to add a few get methods

public String getName()
{
  return Name;
}

public int getAge()
{
  return Age;
}

public int getId()
{
  return Id;
}

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.