11 Replies Latest reply: Apr 10, 2012 4:23 AM by 927536 RSS

    How to use a function in a return value

    927536
      Hello I have a function in my code which is;
      public float CalNum( String Text ){
           
           String[] tokens = Text.split("[ ]+");
           // System.out.println(tokens.length);
           for (int i = 0; i < tokens.length; i++) {
               if (i == 2) {
                    System.out.println(tokens); //Testing of the Token that the number is gathered.
                float ValueSTR = Float.valueOf(tokens[i].trim()).floatValue();
                //System.out.println(Value);
                
           }
           }
           return ValueSTR;
      }
      In this code i am trying to set the "ValueSTR" inside the if statement to make it the return to the software. 
      
      It gives me some errors. 
      
      In need of help please. 
      
      ömer kaya
      METU-GGIT
      
      Edited by: EJP on 6/04/2012 17:50: added { code } tags. Please use them, for readablilty's sake. Otherwise no-one will read your code.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: How to use a function in a return value
          EJP
          It gives me some errors.
          It gives you what errors?
          • 2. Re: How to use a function in a return value
            927536
            Hello thanks for the reply

            ValueSTR; variable it is not getting it inside the for loop or if statement.

            it says;

            "ValueSTR cannot resolved to a variable. "

            the given error is that.

            ömer kaya
            METU-GGIT
            • 3. Re: How to use a function in a return value
              EJP
              That's because you declared it two scopes in from where you are using it in the return statement. If you need it in the scope of the return statement it must be declared in that scope, or a wider scope. However instead of declaring it at all you could just return the actual value, with a 'return null' at the bottom to cover the not-found case.

              I am curious what you think this has to do with 'using a function in a return value'. It is the other way around at the moment.
              • 4. Re: How to use a function in a return value
                927536
                Hello Thanks for the reply.

                Here is what i have done;

                In the Program's main part i did;

                Float Alpha = CalNum(AlphaString);
                System.out.println(Alpha);

                In the function;
                public float CalNum( String Text ){
                     
                     String[] tokens = Text.split("[ ]+");
                     // System.out.println(tokens.length);
                     for (int i = 0; i < tokens.length; i++) {
                         if (i == 2) {
                              //System.out.println(tokens); //Testing of the Token that the number is gathered.
                          float ValueSTR = Float.valueOf(tokens[i].trim()).floatValue();
                          //System.out.println(Value);
                          
                     }
                     }
                     return (Float) null;     
                     
                }
                But what i get is;
                Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
                     at CamCalib.CalNum(CamCalib.java:373) --> return (Float) null;
                     at CamCalib$6.actionPerformed(CamCalib.java:286) --> Float Alpha = CalNum(AlphaString);
                     at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
                Have any ideas? 
                
                Sorry for the topic inconvenience;
                
                ömer kaya
                METU-GGIT
                
                Edited by: Ömer K. on Apr 10, 2012 1:59 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                • 5. Re: How to use a function in a return value
                  EJP
                  You don't appear to have understood my suggestion. When I say 'return the actual value' I don't mean 'declare a new variable that is initialized to the new value and then immediately falls out of scope'.
                  • 6. Re: How to use a function in a return value
                    927536
                    Thanks for the direction;
                    I have changed as this;
                    public float CalNum( String Text ){
                         
                         String[] tokens = Text.split("[ ]+");
                         // System.out.println(tokens.length);
                         for (int i = 0; i < tokens.length; i++) {
                             if (i == 2) {
                                  //System.out.println(tokens); //Testing of the Token that the number is gathered.
                              float ValueSTR = Float.valueOf(tokens[i].trim()).floatValue();
                              return ValueSTR;
                              //System.out.println(Value);
                              
                         }
                         }     
                         
                    }
                    but unfortunately it does not compile;
                    
                    problem: This method must return a result type float
                    
                    Any ideas? 
                    
                    ömer kaya
                    METU-GGIT
                    
                    Edited by: Ömer K. on Apr 10, 2012 1:59 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                    • 7. Re: How to use a function in a return value
                      798692
                      KorcaeL wrote:
                      but unfortunately it does not compile;

                      problem: This method must return a result type float

                      Any ideas?

                      ömer kaya
                      METU-GGIT
                      What would happen if the if condition is always false? By definition of the function CalNum, it must return a float value.

                      Here is a simple example,
                      int methodA(int a) {
                           int returnValue = 0;
                           if(a>0) {
                                 returnValue++;
                           }
                           return returnValue;
                      }
                      • 8. Re: How to use a function in a return value
                        927536
                        Found it...
                        public  float CalNum( String Text ){
                             
                             String[] tokens = Text.split("[ ]+");
                             float ValueSTR = 0;
                             // System.out.println(tokens.length);
                             for (int i = 0; i < tokens.length; i++) {
                                 if (i == 2) {
                                      //System.out.println(tokens); //Testing of the Token that the number is gathered.
                                  ValueSTR = Float.valueOf(tokens[i].trim()).floatValue();
                                  
                                  //System.out.println(ValueSTR);
                                  
                             }
                             }
                             return ValueSTR;
                             
                        }
                        is the last style... 
                        
                        thanks for the help.
                        
                        ömer kaya
                        METU-GGIT
                        
                        Edited by: Ömer K. on Apr 10, 2012 2:00 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                        • 9. Re: How to use a function in a return value
                          927536
                          To use the return type it must be declared in the method body and it must be set as return in the method body not in the if statement.

                          Changing that it returns the value...
                          • 10. Re: How to use a function in a return value
                            EJP
                            Found it...
                            Not really. Why the loop? when all you are doing is using i==2?
                            public  float CalNum(String Text) {
                                if (tokens.length > 2)
                                  return Float.valueOf(tokens[2].trim()).floatValue();
                                return null;
                            }
                            • 11. Re: How to use a function in a return value
                              927536
                              Hello;

                              my code was a replica, for what i have needed;

                              there is a String Change in the Text that i am trying to parse.

                              It can be changed with a variety of values like "=" , "[ ]+" or "+-"

                              so that the loop is for the parsing. i could have deleted when posting but came lazy. so just posted.

                              ömer kaya
                              METU-GGIT

                              Edited by: Ömer K. on Apr 10, 2012 2:23 AM