Results 1 to 2 of 2
  1. #1
    Azaera is offline Member
    Join Date
    Jun 2012
    Location
    Gothenburg, Sweden
    Posts
    5
    Rep Power
    0

    Default linking two lists to each other

    Hi.

    I have a small problem with being able to link two lists of objects to each other. I've tried the solution provided here which gave me error with the + operator.

    What I have is two ArrayLists of Objects where Accounts keeps the details of the accountnumber while the Customer keeps the details of name and personalnumber (pNr). I have to link in the Account list the accountnumber with the personalnumber in the Customer list.

    I also tried to set it up with an HashMap which did succeed until I had to print it where I got all objects out when I just wanted one object at a time ( depending on a condition ).

    When I do the final iteration based on if the pNr is the same as in the stored HashMap, it decides to print out all three objects in the hashmap, even if the number only equals on one Object in it.

    Would you mind to have a look on the code provided and kick me a bit in right direction to get it right please.

    Java Code:
    	private List<Customer> customers = new ArrayList<Customer>();
    	private List<Account> accounts = new ArrayList<Account>();
    	private HashMap<Customer, Account> customerInfo = new HashMap<Customer, Account>(); 
    
           public String infoCustomer(long pNr) {
    		String output = "";
    		
    		
    		Iterator<Customer> custIter = customers.iterator();
    		Iterator<Account> accIter = accounts.iterator();
    		
    		while (custIter.hasNext() && accIter.hasNext()) {
    			customerInfo.put(custIter.next(), accIter.next());
    		}
    		
    		for ( Customer search : customers ) {
    			if (search.getPNr() == pNr) {
    				for ( Map.Entry entry: customerInfo.entrySet() ) {
    					output += entry.getKey() + "" + entry.getValue();
    				}
    			}
    		}
    		
    		output += "\n";
    		return output;
    	}
    When running the file, the output becomes:

    # UTSKRIFT AV KUND 8505221898
    Namn: Pelle Persson. Personnummer: 6911258876. Konto: 1002 Namn: Kalle Karlsson. Personnummer: 8505221898. Konto: 1001 Namn: Lotta Larsson. Personnummer: 7505121231. Konto: 1003


    # UTSKRIFT AV KUND 6911258876
    Namn: Pelle Persson. Personnummer: 6911258876. Konto: 1002 Namn: Kalle Karlsson. Personnummer: 8505221898. Konto: 1001 Namn: Lotta Larsson. Personnummer: 7505121231. Konto: 1003


    # UTSKRIFT AV KUND 7505121231
    Namn: Pelle Persson. Personnummer: 6911258876. Konto: 1002 Namn: Kalle Karlsson. Personnummer: 8505221898. Konto: 1001 Namn: Lotta Larsson. Personnummer: 7505121231. Konto: 1003

    I would the person whos personalnumber is asked to be printed and not the other two persons too.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: linking two lists to each other

    Your line 12-13 adds every single set of customer/account data into the hashmap. Your line 18 then iterates through every single pair in that collection. If you only want 1 record, then don't do that - only return the record you care about.

Similar Threads

  1. linking jframe
    By vinodpatole in forum NetBeans
    Replies: 4
    Last Post: 01-20-2012, 11:15 PM
  2. Replies: 3
    Last Post: 06-07-2011, 05:40 AM
  3. Help with linking classes please
    By tenhotshot in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 04:38 AM
  4. static linking
    By Nicholas Jordan in forum Advanced Java
    Replies: 35
    Last Post: 03-14-2009, 09:17 PM
  5. linking 2 Jframes
    By suhaib1thariq in forum New To Java
    Replies: 9
    Last Post: 02-13-2009, 01:30 AM

Posting Permissions

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