0

I'm trying to create a recall program that sends text messages to 200+ people and then searches an email that the replies are forwarded too.

This method is supposed to search the array list of replies that is built using another method, but it doesn't work correctly. It will only work if the very first message on the array list matches the very first number in the contact list.

Those are some other problems, but my main question here is why does it say that the code specifically inside of my for loop is dead code?

public static boolean searchForPhone(String phone){
         CharSequence phoneN = phone;
         for(int i=0;i<myMessages.size();i++){
                if(myMessages.get(i).contains(phone)){
                    return true;
                }
                else{ 
                   return false;
                }
         }
         return false;
        }

5 Answers5

2

This is your code, properly formatted:

public static boolean searchForPhone(String phone) {
  for (int i = 0; i < myMessages.size(); i++) {
    if (myMessages.get(i).contains(phone)) {
      return true;
    } else {
      return false;
    }
  }
  return false;
}

The construct flagged as Dead code is the i++ in the for-loop header. It is indeed dead code because the for loop's body unconditionally makes the method return. Therefore the "step" part of the for header is unreachable aka. dead.

The same fact makes your code perform incorrectly, BTW. Removing the else clause would be a big improvement.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • +1 for clarifying the *increment statement(s)* part of the loop header as the dead code area – ifloop Oct 22 '14 at 11:37
1

Your loop always returns from the function at the end of the first iteration. This makes i++ dead code since it never executes.

Anyway, remove the else clause to fix the code.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @Naveen That is what. There are chances that, no way entering in to loop. Hence a default return must be there. Try it. NPE got the actual point. +1. – Suresh Atta Oct 22 '14 at 11:26
  • The loop will never finish it's first iteration because of the `return` statements in both, the `if` and the `else` part. So the code after that (especially the *increment statement* of the loop header) is dead, not the code after the loop (because if `mMessages.size()` returns 0, the loop will not be executed but **the code after the loop** will be). – ifloop Oct 22 '14 at 11:34
1

Will this help?

public static boolean searchForPhone(String phone){
     CharSequence phoneN = phone;
     for(int i=0;i<myMessages.size();i++){
            if(myMessages.get(i).contains(phone)){
                return true;
            }
     }
     return false;
    }

Look you are looping over n-element list. When you get first element on the list you got if/else statement. So you will HAVE TO either of 2 things, both of witch is return. So your program will exit on first element returned.

To make it simplier, your code is equal to:

 CharSequence phoneN = phone;
 if (myMessages.size() ==0 ){
 return false;
 }

 return myMessages.get(0).contains(phone);
Beri
  • 11,470
  • 4
  • 35
  • 57
1

Try from Window > Preferences > Java > Compiler > Error/Warnings

Change Dead code (e.g 'if(false)') and Unnecessary 'else' statement to Error.

Vitkinov
  • 79
  • 6
0

In the else part you need to continue to search. Else if your fist element is not the matching one will return false and not going to check other element.

 public static boolean searchForPhone(String phone) {
    CharSequence phoneN = phone;
       for (int i = 0; i < myMessages.size(); i++) {
          if (myMessages.get(i).contains(phone)) {
             return true;
           } else {
             //return false this conditional return cause 
             // the complain it as dead code. Since for loop will become not
             //loop
             continue; // will search for other elements.
           }
        }
      return false;
 }

Now you can simplify this code to following because else part is not really necessary.

public static boolean searchForPhone(String phone) {
    CharSequence phoneN = phone;
    for (int i = 0; i < myMessages.size(); i++) {
        if (myMessages.get(i).contains(phone)) {
            return true;
        } 
    }
    return false;
 }   
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115