0

Ok so this is what I have

public class Register { public String propertyID; public String PPSNumber; Register(String aPropertyID, String aPPSNumber) { propertyID = aPropertyID; PPSNumber = aPPSNumber; } public void setPPSNumber(String aPPSNumber) { PPSNumber = aPPSNumber; } public String getPPSNumber() { return PPSNumber; } public String getPropertyID() { return propertyID; } } 

Then I have this

public static ArrayList<Register> registers = new ArrayList<Register>(); public static void main(String[] args) { String userInput1 = "", userInput2 = "", userInput3 = ""; userInput1 = JOptionPane.showInputDialog("Enter your PPSNumber"); userInput2 = JOptionPane.showInputDialog("Enter your propID"); registers.add("number", "id"); } 

I don't understand why It wont let me add to the ArrayList. Is there some way of adding class types to ArrayLists?

1
  • How does one print out the arraylist registers. Also how do you add multiple registers eg. multiple ppsnumbers and ids? Commented Apr 20, 2013 at 20:49

2 Answers 2

4

Try this instead :

registers.add(new Register("number","id")); 

EDIT 1: To answer your question, you can create a separate "register" and the use the getters :

Register aRegister = new Register("number","id"); registers.add(aRegister); System.out.println(aRegister.getPropertyID()+" "+ aRegister.getPPSNumber()); 
Sign up to request clarification or add additional context in comments.

3 Comments

How do you print whats inside of register then?
Thanks for the help one more question. Classes are new to me hence all the questions. I will be reading in alot of IDs and PPSNumbers into the registers arraylist. But I dont want to have to creat a new register (aRegister) for each id/property, that wouldnt work. How do I do that. Thanks again.
@user2079139: Why wouldn't it work? It would work without any problem. Do that.
0

Your List is of type Register so you need to add object of Register class only.

Nothing wrong in create as many Register objects as required.

You can implement toString() method inside Register class then the below sysout will work given the register variable is initialized with Register object. Check this How to override toString() properly in Java? to know about toString implementation.

System.out.println(register) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.