1 2 Previous Next 22 Replies Latest reply: Jun 14, 2012 5:01 AM by 800268 RSS

    Method "jumping" return statement

    Daniel_Meza
      (Sorry for my bad English)
      Hi, i have a method that makes comparison between Strings, here it is :
          public String comparacion(String cc,String ci,int tlnm,int ndc){               
                                                
                    int te = ci.indexOf(' ');                                          
                    
                    if( te != -1 ){
                                          
                        String[] palabras = ci.split(" "); 
                        
                        int index = palabras.length - 1;                                                                                                                     
                                
                        String nombre = "";                                     
                        int indice = 0;                
                                             
                        for( int k = 0; k <= index; k++ ){                                               
                             for( int lcc = cc.length() - 1,lci = palabras[k].length() - 1 ;
                                      lci >= 0 && lcc >= 0;
                                      lcc--,lci-- ){
                                                           
                                  char ccc = cc.charAt(lcc);
                                  char cci = palabras[k].charAt(lci); 
                                  
                                  if( ccc != cci ){ 
                                                                                                      
                                      break;                                
                                      
                                  }
                                  
                                  if( "DE".equals(palabras[k]) || "DEL".equals(palabras[k]) || 
                                      "LA".equals(palabras[k]) || "LOS".equals(palabras[k]) ||
                                      "SAN".equals(palabras[k]) ){
                                      indice++;
                                      //System.out.println(index + " " + indice );                                    
                                      break;
                                  }
                                  
                                  if( ccc == cci ){                      
                                      
                                      if( lci == 0 ){                                                                                            
                                          nombre += palabras[k] + " ";                                                      
                                          indice++;                                    
                                          //System.out.println("Afuera = " + index + " " + indice + " " + nombre);                                                                   
                                      }                                                                
                                                                                                                                                                                                                                                                                                           
                                  }
                                  
                                      
                                  if( indice == index ){ 
                                      System.out.println(index + " " + indice);
                                      return null; 
                                  }
                                                                                                                      
                                  if( tlnm - 1 == ndc  ){                                    
                                      //System.out.println(index + " " + indice );                                    
                                      System.out.println("Posible nombre mal escrito en el catalogo : " + ci );                                        
                                  }                            
                                                                                                                                                                                                                                                                                                                         
                             }
                             
                        }                                    
                                          
                    }
                    
                    if( te == -1 ){                                       
                         
                        for( int lcc = cc.length() - 1,lci = ci.length() - 1; lci >= 0 && lcc >= 0; lcc--, lci-- ){
                         
                             char ccc = cc.charAt(lcc);
                             char cci = ci.charAt(lci); 
                                     
                             if( ccc != cci ){
                                 //Son diferentes
                                 //System.out.println(cci + " es diferente de " + ccc);
                                 //return "Es doble supongo : " + cc ;                                      
                                 break;
                             }    
                                          
                             if( ccc == cci ){                      
                                 //System.out.println(cci + " " + ccc);
                                 if( lci == 0 ){ 
                                     //System.out.println("Son iguales : " + ci + " " + cc);                                       
                                     //return "Son iguales : " + ci + " y " + cc;                                
                                 }                                                                                                                                     
                            
                             }        
                                     
                        }
                                                      
                       
                    }
                     
                    return null;
                     
             }
      The problem is in the if that evaluates if indice is equal as index if true and if my understanding of the return statement is right, then it should stop there and return to the
      function where it was called from right? Unfortunately this is not happening, in fact i tested with the System.out.println and it enters when the condition is true but it seems
      it skips the return and keeps going on the rest of the code. Any idea of what could be the issue ?

      -Daniel
        • 1. Re: Method "jumping" return statement
          EJP
          Hard to believe, in fact impossible. Does this print appear?
          System.out.println(index + " " + indice);
          • 2. Re: Method "jumping" return statement
            Daniel_Meza
            Hi EJP, yes the print appears.
            • 3. Re: Method "jumping" return statement
              Daniel_Meza
              And forget to mention, I'm using netbeans 7.2(beta) with jdk 1.7 update 4.
              • 4. Re: Method "jumping" return statement
                EJP
                Sorry, I don't believe that return isn't returning. What evidence do you have?
                • 5. Re: Method "jumping" return statement
                  Daniel_Meza
                  Hi EJP I've changed the part of the if with the return to this:
                     if( indice == index ){                                 
                          System.out.println("This is index = " + index + " and indice = " + indice);
                          return null; 
                          //break;
                     }
                  and this is the output :
                  This is index = 1 and indice = 1
                  This is index = 1 and indice = 1
                  This is index = 1 and indice = 1 ...
                  This is index = 1 and indice = 1
                  This is index = 1 and indice = 1
                  as you can see the equality is there but the return never touched, I'm starting to thinking is a bug of netbeans maybe the jdk actually don't really
                  know.Hope someone can help me.
                  • 6. Re: Method "jumping" return statement
                    EJP
                    That just suggests that the method is being called a number of times.
                    • 7. Re: Method "jumping" return statement
                      rp0428
                      >
                      as you can see the equality is there but the return never touched, I'm starting to thinking is a bug of netbeans maybe the jdk actually don't really
                      know.Hope someone can help me.
                      >
                      Seems like a simple test should tell whether the 'return null' is being skipped. Currently you have
                      if( indice == index ){                                 
                              System.out.println("This is index = " + index + " and indice = " + indice);
                              return null; 
                              //break;
                         }
                      So add this line AFTER the return statement
                      System.out.println("This is AFTER the 'return null' statement.");
                      And post whether or not the new line is printed.
                      • 8. Re: Method "jumping" return statement
                        943414
                        I am using your code, Its returning successfully just after printing

                        System.out.println(index + " " + indice);

                        statement.

                        Edited by: 940411 on Jun 13, 2012 5:37 AM
                        • 9. Re: Method "jumping" return statement
                          EJP
                          So add this line AFTER the return statement
                          It won't compile, being unreachable.
                          • 10. Re: Method "jumping" return statement
                            TPD-Opitz
                            Hello Daniel,

                            IMHO your code has several issues wich make it hard to read and therefore hard to debug:

                            1. choose better variable names. Variable names should clearly express what they contain or what meaning they have. e.g.:
                            int te = ci.indexOf(' '); // bad
                            int indexOfSpace = ci.indexOf(' '); // better
                            // but in your case it really should have been:
                            boolean isSingleWord = -1 == ci.indexOf(' ');
                            2. you schould never return null unless null is a valid member of the result set (and not some kind of error indikator).
                            I understand that you plan to have it return something but my impression is that String meight not be the appropriate return type. The other option is that the name of this method is not appropriate.

                            3. what keep you from using <b>StringTokenizer</b>?

                            4. why do you iterate over the single characters yourself instead of relying on <b>String.equals()</b>?

                            5. the line
                            if( te == -1 ) 
                            expresses the opposit of the first <b>if</b>, so you should turn it into <b>else</b>.

                            6. you should extract the bodies of <tt>if( te != -1 )</tt> and <tt>if( te == -1 )</tt> (after turning it to else) to their own methods.

                            7. the part
                             if( "DE".equals(palabras[k]) || "DEL".equals(palabras[k])
                                  "LA".equals(palabras[k]) || "LOS".equals(palabras[k]) ||
                                  "SAN".equals(palabras[k]) )
                            should better be written this way:
                            private static final List<String> KEYWORD_LIST = Arrays.asList(new String[]{ "DE","DEL","LA", "LOS","SAN"]);
                            ...
                            if(KEYWORD_LIST.contains(palabras[k]))
                            8. The keyword check that does not depend on anything declared in the inner loop so it shuld be moved out of it.

                            9. the check of the input parameters tlnm and ndc do not depent on anything in both of the loops so it should be moved behind (or before) them.

                            10.
                            if( ccc != cci ){ 
                                break;                                
                            }
                            // ...
                            if( ccc == cci ){
                              // ...
                            }
                            again this is the opposit so instead of breaking the loop you should have formed a better <b>if</b>.


                            hope this helps.
                            bye
                            TPD
                            • 11. Re: Method "jumping" return statement
                              800268
                              T.PD wrote:
                               private static final List<String> KEYWORD_LIST = Arrays.asList(new String[]{ "DE","DEL","LA", "LOS","SAN"]);
                              Arrays.asList takes a varargs, so you can just do:
                              private static final List<String> KEYWORD_LIST = Arrays.asList("DE", "DEL", "LA", "LOS", "SAN"); 
                              However, since the main reason is to do a contains() on it a Set would be better. No handy method for that though, so you get:
                              private static final Set<String> KEYWORD_LIST = new HashSet<String>();
                              // or perhaps a new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) is more useful in this case
                              static {
                                Collections.addAll(KEYWORD_LIST, "DE", "DEL", "LA", "LOS", "SAN");
                              } 
                              • 12. Re: Method "jumping" return statement
                                TPD-Opitz
                                Walter Laan wrote:
                                Arrays.asList takes a varargs, so you can just do:
                                private static final List<String> KEYWORD_LIST = Arrays.asList("DE", "DEL", "LA", "LOS", "SAN"); 
                                Last time i tried
                                private static final List<String> KEYWORD_LIST = Arrays.asList({"DE", "DEL", "LA", "LOS", "SAN"}); 
                                which gave me a compile error. Thanks for that hint.
                                However, since the main reason is to do a contains() on it a Set would be better.
                                No doubt.
                                *[edit]:* found that interface java.util.Collection would fit perfectly as type of the constant since it defines <tt>contains()</tt> and it's implemented by List, HashSet and TreeSet.
                                On the right hand side a list should be fine until the count of the keywords reaches a significant number...
                                No handy method for that though,...
                                I rather have the "wrong" Collection then the ugly static initializer. But that's a question of taste I guess...

                                bye
                                TPD

                                Edited by: T.PD on 13.06.2012 17:24
                                • 13. Re: Method "jumping" return statement
                                  rp0428
                                  >
                                  It won't compile, being unreachable.
                                  >
                                  I know that - I was asking OP to do that to see if OP reports that the code actually being used won't compile. Too many times posted code has been sanitized or otherwise doesn't match what is actually being used. If OP reported that it printed that line we would know the posted code isn't what is being executed.
                                  • 14. Re: Method "jumping" return statement
                                    gimbal2
                                    Seems like firing up the old debugger and stepping through the code line for line would take away any need for discussion and guessing, but I'm silly like that.
                                    1 2 Previous Next