0

I'm trying to select and get values from customer objects. What I want to do is to enter a personal number like "702312" and search after the customer objects that has a data member personal number that are equal to "702312". And when I found it I want to get the rest of the values or change it's content. This is some code that creates the customer objects from the class Customer and then it's stored in a arraylist.

// create a new customer object and send personal number, name and account number to constructor
Customer customer = new Customer(personalNumber, name, newAccountNumber);

// create an arraylist to store customer objects
ArrayList<Customer> customerList = new ArrayList<Customer>();

// add the new customer object to arraylist that holds all customer objects
customerList.add(customer);

I have tried to reach the values like this, but it's not working, so I'm looking for some help?

 // search for customer
 for (Customer customer : customerList) {
if(customer.getAccountOwnerPersonalNumber() == "702312"){
    System.out.println("OK!!!");
    }
 }

And instead of:

 if(customer.getAccountOwnerPersonalNumber() == "702312")...

I have tried this:

 if(customer.personalNumber == "702312")...

Finally I have also tested it like this:

for(int i=0;i<customerList.size();i++){
    if(customerList.get(i).getAccountOwnerName() == "702312");
    System.out.println("OK");
    break;
}

I'm not sure if I'm doing right!? Help is preciated! Thanks!

2
  • what type returns .getAccountOwnerName() function? i think you are trying to compare number and string... Commented Nov 24, 2011 at 14:34
  • You are not calling the System.out.println method correctly. Spaces must surround the parentheses like so: System.out.println ( "OK" ) ; Commented Nov 24, 2011 at 14:38

6 Answers 6

5

You need to use the equals() method to compare objects like Strings by their internal value (otherwise they will only be compared by reference):

if (customer.getAccountOwnerPersonalNumber().equals("702312")) {
    System.out.println("OK!!!");
}

or, better, if it can potentially return null:

if ("702312".equals(customer.getAccountOwnerPersonalNumber())) {
    System.out.println("OK!!!");
}

or, if applicable, just make it a primitive like int, so that == will work the way you intended:

private int accountOwnerPersonalNumber;

with

if (customer.getAccountOwnerPersonalNumber() == 702312) {
    System.out.println("OK!!!");
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! With equals it worked fine! But how do I get the rest of the objects data member in the object that has personal Number equal "702312" ?
Just collect them in another List inside the loop. Ask a new question if you stucks.
I'm not sure I understand your answer and I'm stuck! Do you have time to show a bit of code?
Create a new List<Customer> before the loop, add the matching customers to that list inside the if block of the loop, exactly there where you've put the System.out.println("OK!!!").
After some thinking, would it not be easier to do something like this: System.out.println(customerlist.get(i).getCustomerName(); to use the value of i as a index to the arraylist ??
|
1

You want .equals():

if(customerList.get(i).getAccountOwnerName().equals("702312"))

Comments

1

customer.getAccountOwnerPersonalNumber() == "702312" this is wrong you cant compare them like that because it will compare their object type. you should do like this

if(customer.getAccountOwnerPersonalNumber().equals("702312"))

or this is better

if("702312".equals(customer.getAccountOwnerPersonalNumber()))

Comments

0

If the data that you query for is a String then in that case you have to use equals()

So the entire things will turn out to be

if(customer.getAccountOwnerPersonalNumber().equals("702312")) { System.out.println("OK!!!"); }

Comments

0

You're trying to compare customer.personalNumber to "702312" which is a String, so i conclude customer.personalNumber is a String too. If I'm right, try String.equals() method instead of == operator. This is because this operator compares objects instead of value, which is what you want.

if(customer.getAccountOwnerPersonalNumber().equals("70312");)
  ...

Comments

0

I would suggest you use java.util.Collections binarySearch(...) method. It's of order O(log n)(in this case, since ArrayList implements RandomAccess) which is tons better than the O(n) order you get from doing your for loop.

You can make a separate comparator for the desired Customer fields(s) (in this case "personalNumber"):

private static class CustomerPersonalNumberComparator implements Comparator<Customer> {

        @Override
        public int compare(Customer o1, Customer o2) {
            return o1.getPersonaNumber().compareTo(o2.getPersonaNumber());
        }

    }

Also, don't forget to sort your list according to your desired search criteria (the Comparator) before using binary search on it:

Collections.sort(customerList,new CustomerPersonalNumberComparator());
int desiredIndex = Collections.binarySearch(customerList, new Customer("658",null,null), new CustomerPersonalNumberComparator());
Customer desiredCustomer = desiredIndex == -1 ?  null : customerList.get(desiredIndex);

And don't forget that the binary search returns the position inside our array for the desired element or -1 if nothing was found. So you must use that returned position to retrieve the desired instance from the list.

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.