Results 1 to 5 of 5
  1. #1
    anders73 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Problem with user defined class in ArrayList

    Hi
    I am trying to create a phonebook. I am not the first according to my searches online...I am trying to utilize "ArrayList" and it works fine, the objects( Person (String name, String number)) are stored and I can fetch them, but the list can't test if this object already is stored or not, or at least, the test always returns true (go ahead, store the new person).
    Red marks where the test takes place. can anyone tell me why the test fails?
    edit:I do not get any errors on this code, just that the test doesnt work...
    ///////////
    Java Code:
     
    import se.lth.cs.pt.io.*;//This is from a .jar
    import java.util.*;
    public class Phonebook {
    	public static void main(String[] args) {
    		new Phonebook().menu();
    	}
    	//Working menu that only takes digits between 0 and five
    	void menu() {
    		System.out.print("Vad vill du göra: \n" +
    				"1. Lägg in ny person. \n" +
    				"2. Tag bort person. \n" +
    				"3. Sök på del av namn. \n" +
    				"4. Se vem som har givet nummer. \n" +
    				"5. Skriv ut alla personer. \n" +
    				"0. Avsluta. \n" +
    				"Ange kommando (0-5): ");
    		int menuChoice;
    		//Checks if it is a digit entered
    		while(!Keyboard.hasNextInt()) {	
    			String string=Keyboard.nextLine();
    			System.out.println("\nOgiltigt val! "+string+"\n");
    			menu();		
    		}
    		menuChoice = Keyboard.nextInt();
    		//Checks if it is a digit in correct interval
    		if(5 < menuChoice) {
    			System.out.println("\nOgiltigt val! "+menuChoice+"\n");
    			menu();
    		}
    
    		//Sends menu choice to sorter
    		choiceSorter(menuChoice);
    	}
    	void choiceSorter(int choice) {
    		switch (choice) {
    		case 0: System.exit(0); break;
    		case 1: newPerson(); break;
    		case 2: removePerson(); break;
    		case 3: searchPerson(); break;
    		case 4: searchNumber(); break;
    		case 5: listPersons(); break;
    		}
    	}
    	Person nPerson;
    	Register register = new Register();
    	void newPerson() {
    		String name = Keyboard.nextLine("Skriv namn på ny kontakt: ");
    		int pnumber = Keyboard.nextInt("Ange nummer: ");
    	    String number = Integer.toString(pnumber);
    	    nPerson = new Person(name,number);
    	    if(!register.insert(nPerson)) {
    	    	System.out.println("Den kontakten finns redan! \n");
    	    }
    	    else {
    	    	System.out.println("OK! \n");
    	    }
    	    //System.out.println((register.get(0)).getName());
    	    menu();
    	}
    	void removePerson() {
    		String name = Keyboard.nextLine("Skriv namnet på personen som ska raderas: ");
    	}
    	void searchPerson() {
    		String name = Keyboard.nextLine("Skriv namn/del av namn på kontakt: ");
    	}
    	void searchNumber() {
    		int number = Keyboard.nextInt("Ange nummer som du vill söka på: ");
    	}
    	void listPersons() {
    		System.out.println(register.size()+"\n");
    		for(int i=0; i<register.size(); i++) {
    			System.out.println((register.get(i)).getName() +" " +(register.get(i)).getNumber()+"\n");
    		}
    		menu();
    	}
    }
    class Person {
    	private String name;
    	private String nbr;
    	Person(String inName, String inNbr) {
    		name=inName;
    		nbr=inNbr;
    		//contacts.add(new contact(name,nbr));
    		//Skapar kontakt emd name och number
    	}
    	public String getName() {
    		//Ger personens namn
    		return name;
    	}
    	public String getNumber() {
    		//ger personens nummer
    		return nbr;
    	}
    }
    class Register {
    	public Person nPerson;
    	public String name;
    	//private String number;
    	public ArrayList<Person> contacts;
    	
    	Register (){
    		//Skapar ett register med plats för objekt av klassen "Person"
    		contacts = new ArrayList<Person>();
    		//nPerson=person;
    		
    	}
    	public int size() {
    		return contacts.size();
    	}
    	[COLOR="Red"]boolean insert(Person nPerson) {
    		//Testar om det finns ett objekt av klassen "Person" som är 
    		name=nPerson.getName();
    		System.out.print(nPerson.getName());
    		if(contacts.contains(nPerson)) {
    			return false;
    		}
    		else {
    			contacts.add(contacts.size(), nPerson);
    			return true;
    		}
    	}[/COLOR]
    	public Person get(int i) {
    		return contacts.get(i);
    	}
    	
    	/*boolean remove(String name) {
    		return contacts.remove(name) != null;
    	}
    	Person containsName(String name) {
    		return contacts.get(name);
    	}
    	List<Person> findByPartOfName(String partOfName) {
    		
    	}
    	List<Person>findByNumber(String nbr) {
    		
    	}
    	public Person[] findAll() {
    		return (Person[]) contacts.values().toArray(new Person[0]);
    	}*/
    }
    Last edited by anders73; 04-26-2011 at 01:16 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Please wrap your code in code tags so we can easily see how it's formatted. Also, ask some specific questions about what your problems are, and supply errors(if any, copy/paste them)

    [code ] <-omit space
    YOUR CODE HERE
    [/code]


    I believe for a call to contains to work correctly you are going to want to give the class some way to test equality. Override equals, and you may want to look into override hashcode as well.

  3. #3
    anders73 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    I'll look into that, seems I've overlooked the possibility of two objects of same class to be different if theire attributes are the same...

  4. #4
    anders73 is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default Not sure how to "override equals and hashcode"...

    I have been reading here
    Overriding the equals and hashCode methods - Java insights
    but I'm not sure how to implement that in my case.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may not need a hashcode, however, you can take advantage of the instance variables. In the classes equals method, if an instance variable is a string you can use strings equals method.

    Java Code:
    class X{
      String x;
      public boolean equals(Object o){
        X x = (X)o;
        return x.equals(x.x);
      }
    }
    You can make your equals method compare two items however you please.

Similar Threads

  1. array of objects with user defined values
    By swathi dharmaraj in forum New To Java
    Replies: 3
    Last Post: 04-14-2011, 03:59 PM
  2. Replies: 6
    Last Post: 01-26-2011, 07:49 PM
  3. Facing problem in compiling user defined package
    By UJJAL DHAR in forum New To Java
    Replies: 2
    Last Post: 05-24-2010, 11:40 AM
  4. User Defined Method
    By overcranked in forum New To Java
    Replies: 6
    Last Post: 04-09-2010, 01:02 AM
  5. JSP with user-defined java classes
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 03-05-2010, 06:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •