-2

I get an error in the code from this part of my code:

public boolean findCustomer(String inPersonalNumber){

    // check if personal number already exist
    for (int i=0; i<customerList.size();i++) {
        if(customerList.get(i).getCustomerPersonalNumber().equals(inPersonalNumber)){
            return true;
        }
    }
    return true;    
}

When I remove the first return true and instead to the last return true, it don't get the error in my eclipse code, but why can't I have the first place and would this be the same? Thanks!

EDIT: The error message from eclipse say: This method must return a result of type boolean. I'm confused because isn't that what I have done?!

Yes, a break must be in the code

Can I write the method in some other way?

EDIT NUMBER 2
Why isn't this code working?

public boolean findCustomer(String inPersonalNumber){

// check if personal number already exist
for (int i=0; i<customerList.size();i++) {
    if(customerList.get(i).getCustomerPersonalNumber().equals(inPersonalNumber)){
            return true;
        }
        else {
            return false;
        }
    }
}

This method returns a boolean value so I don't understand why I get an error!? The code looks right to me?

Julian
  • 20,008
  • 17
  • 77
  • 108
3D-kreativ
  • 9,053
  • 37
  • 102
  • 159
  • 7
    It would perhaps help if you mentioned _what_ error you're getting... And by the way: no matter what, you're function will _always_ return `true`, so there is probably something wrong with your logic as well! – Julian Jan 13 '12 at 12:28
  • Your method compiles fine, so what error are you seeing? – adarshr Jan 13 '12 at 12:29
  • 1
    The last return should be "return false;" as you didn't find the customer. Given the comments above, is it possible that Eclipse is complaining that your code always returns true? – Maarten van Leunen Jan 13 '12 at 12:32
  • I think you have turned off "Build Automatically" feature of eclipse. It maybe complaining about an error that used to be present when you still hadn't typed in your code fully! – adarshr Jan 13 '12 at 12:44
  • @3D-kreativ: are you getting _the same_ error after your 2nd edit, or do you get a different kind of error? Please be more explicit when writing your questions, that makes it _much_ easier for people to understand what exactly your problem might be! – Julian Jan 13 '12 at 12:55
  • Yes I get the same error: "This method must return a result of type boolean" – 3D-kreativ Jan 13 '12 at 13:01

6 Answers6

3

Your edit #2 doesn't compile because there is a possibility that your code won't enter the for-loop. This will be the case if customerList.size() is 0. To fix this, you'll simply need to add a return statement after the for-loop as well:

// check if personal number already exist
for (int i=0; i<customerList.size();i++) {
    if(customerList.get(i).getCustomerPersonalNumber().equals(inPersonalNumber)){
        return true;
    }
    else {
        return false;
    }
}
return false;

Another point here is that this code doesn't logically make much sense: it will only return true or false based on the first item in your list. And this is probably not what you want. So take a closer look at several of the other answer here, many of which are good examples for how you can do this.

Julian
  • 20,008
  • 17
  • 77
  • 108
0

Looks like you have turned off the Build Automatically feature of eclipse. It maybe complaining about an error that used to be present when you still hadn't typed in your code fully! This can also happen if you have back-dated your system for some reason.

Also, shouldn't you be returning false if the condition doesn't satisfy?

public boolean findCustomer(String inPersonalNumber) {

    // check if personal number already exist
    for (int i = 0; i < customerList.size(); i++) {
        if (customerList.get(i).getCustomerPersonalNumber().equals(inPersonalNumber)) {
            return true;
        }
    }

    return false;
}
adarshr
  • 61,315
  • 23
  • 138
  • 167
  • 2
    How do you know what he wants? He just asks why he is getting error.Maybe he just want to know how eclipse works. – shift66 Jan 13 '12 at 12:37
  • 1
    I didn't downvote, but it doesn't address the question, just points out a potential business logic issue. It also looks like you were the one that downvoted answers with a"break" (whether or not you did), and those downvotes would be inappropriate if the"break"was the reason. – Dave Newton Jan 13 '12 at 12:39
  • @adarshr I didn't, and specifically said as much. – Dave Newton Jan 13 '12 at 12:46
0
public boolean findCustomer(String inPersonalNumber){
    boolean result = false;
    // check if personal number already exist
    for (int i=0; i<customerList.size();i++) {
        if(customerList.get(i).getCustomerPersonalNumber().equals(inPersonalNumber)){
            result = true;
            break;
        }
    }
    return result ;    
}
bpgergo
  • 15,669
  • 5
  • 44
  • 68
  • 2
    I don't feel this is an improvement over just returning. – Dave Newton Jan 13 '12 at 12:31
  • Moreover it's a bad style of coding. Return is more explicit. – yatskevich Jan 13 '12 at 12:35
  • 1
    @IvanYatskevich: whether it's bad style of coding or not, is _higly_ subjective. Having your code cluttered up with lots of `return statements` can turn out quite bad too (I'm not saying that this is one of those cases thouggh)! – Julian Jan 13 '12 at 12:39
  • For me it is easier to read and understand code if I can be sure there will be exactly one `return` statement at the end of the method. Apparently, not everyone prefers this style http://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement – bpgergo Jan 13 '12 at 12:45
0

First return will return only in case of all conditions satisfied, but this method should be returning boolean as per code. It would be expecting a return in failure case also.

Removing first return won't affect compilation as it has a return in second place which will work without any condtions.

Edit : Answer for your second question

This code has two return's, but what if your customerList is size 0, in that case also, method must return boolean. right? for that only, compiler is asking.

BTW, code doesn't have null checks.

Your final code could be this. Keeping multiple return statements in code in not a good practice.

public boolean findCustomer(String inPersonalNumber) {
    boolean retVal = false;
    if (!(inPersonalNumber == null || inPersonalNumber.trim().equals("")
         || customerList == null || customerList.size() == 0)) { // inputs are valid to run this check
         // check if personal number already exist
        for (int i = 0; i < customerList.size(); i++) {
            if (inPersonalNumber.equals(customerList.get(i).getCustomerPersonalNumber()) { // to avoid NPE, kept inPersonalNumber in check 
                retVal = true;
                break;
            }
        }
    }
    return retVal;    
}
Vaandu
  • 4,857
  • 12
  • 49
  • 75
0

When I remove the first return true and instead to the last return true, it don't get the error in my eclipse code, but why can't I have the first place and would this be the same?

If you remove the second return statement the code would be able to run and not return a value - this is not possible as you defined the method to have a return type of Boolean. So it must always return a value no matter what.

Just change the second return statement to false, should do what you want.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • What path wouldn't return a value? – Dave Newton Jan 13 '12 at 12:35
  • @DaveNewton if he removed the second return statement, that is what I think the question was attempting to ask – NimChimpsky Jan 13 '12 at 12:37
  • I thought he said the error happened with the code shown, that's what the first sentence says. – Dave Newton Jan 13 '12 at 12:41
  • @DaveNewton I quoted directly the sentence I felt was relevant.I still think my answer is valid, given the question. – NimChimpsky Jan 13 '12 at 12:43
  • The part where he says with different code there *isn't* an error? Interesting take on it, but ok. – Dave Newton Jan 13 '12 at 12:50
  • @DaveNewton why is it interesting ? The sentence implies the opposite action (removing the second return statement) caused an error. – NimChimpsky Jan 13 '12 at 12:59
  • Because my approach is usually to start off believing what they say. YMMV; carry on. – Dave Newton Jan 13 '12 at 13:09
  • @DaveNewton I don't understand what you are getting at. I do believe what was said : "when I remove the first return instead of the last return it don't get the error". This implies they tried removing the last return statement and received an error. "Ymmv; carry on" er what ? – NimChimpsky Jan 13 '12 at 13:17
  • I know you don't, which confuses me, but that's okay. **Y** our **M** ileage **M** ay **V** ary: it means "you may prefer a different approach". The "carry on" means "whatever, meh, I don't know why you're still talking about this." – Dave Newton Jan 13 '12 at 13:25
-1

Because your for loop looses meaning if you're returning true anyway.
If you want to stop loop use break; instead of first return.

shift66
  • 11,760
  • 13
  • 50
  • 83