Results 1 to 18 of 18
  1. #1
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post [SOLVED] Multiple return values

    Is it possible to have multiple return values in the same method???

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Directly, no. But you can return an array which can hold as many return values as you you want.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Thanks man!

  4. #4
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Quote Originally Posted by Manfizy View Post
    Thanks man!
    Hi, i tried using the array in return and it just worked fine BUT now i come across another problem where i get an error "non-static method cannot be referenced from a static context"
    whats all this about coz i really dont understand why am getting this?


    Best Regards
    Frizy

  5. #5
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    Hi,
    Create an instance and call the method.

    -Regards
    Ramya
    Ramya:cool:

  6. #6
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    Hi pple,
    Am trying to return an array but am not getting any value returned. I really dont know where i made a mistake:confused:

    below is my code:

    Java Code:
    public int iodineChange(double IFY, double MFX, double IDC, double NF){ 
    
    double equilibriumIod = equilibriumIodine(IFY,MFX,IDC);
             
    DecimalFormat myVal = new DecimalFormat("0.00E0");
     
    /*---------------------------------------------------------------------*/
    /*Formulae:  I(t+dt) = I(t) * exp(-IDC * dt) + I(eq) *                 */
    /*                  (1 - exp(-IDC * dt))                               */
    /*---------------------------------------------------------------------*/
        
    int rCount = Xenon.getRowCount();
    int arraySize = rCount;
    int[] iodine_t = new int[arraySize];
    for (int i = 0; i < rCount; i++) {    
         
    String time = (String)Xenon.getValueAt(i,0);
    if (time == null){
    break;
    }   
    
    double time_hrs  = Double.parseDouble(time);
    double time_mins = (time_hrs*60)/300;
    
    double term1 = IDC * time_mins * (-1.0);
    term1 = Math.exp(term1);
          
              
    double Iod = 1.0 - term1;
    Iod = Iod * equilibriumIod;
    
    //calculates instantaneous iodine concentration
    String pr_t = (String)Xenon.getValueAt(i, 1);
    Double powerlevel = Double.parseDouble(pr_t);
    
    double val = IFY;
    val = val * MFX;
    val = val / IDC;
    val = (val * NF * powerlevel);
    
    Iod = Iod + (term1 * val);
    
    Iod = iodine_t[arraySize-1];
    Temp.setValueAt(myVal.format(Iod),i,2); 
    
    
     }
    System.out.println("IODINE CHANGE : "+iodine_t);
    return iodine_t[arraySize-1];
    }

    Any help will be highly appriciated

    Thank you

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Not sure how much of what you want this will fix, but here goes...

    1. change the int in the first line to int[]
    2. Change the return statement to return iodine_t

    The second line of code seems fishy to me... though I may just be missing something.

    BTW... This may not work. We will need to see more code, and an SSCCE would be good if the code is too long to be posted in its entirety (include in a .zip file or .jar(though I don't know if .jar is accepted))

    If for any reason the code cannot be attached or posted, at least explain what Xenon is.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  8. #8
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    ok, this is just a method that reads some values from a table called xenon and does calculation on each value read from the table.

    Java Code:
    int rCount = Xenon.getRowCount();
    gets the number of rows in the table xenon.
    xenon table contains two columns-time and power level

    i want the array size to as big as the number of rows in the table coz the they keep on varrying depending on user input.

    for each row some computation is done and now my problem is returning the all the results obtained in each row from the calculation.

    i hope i am clear enough

    Best Regards,
    manfizy

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    Hi,
    Where you are using the array "iodine_t" .You have to write the comments in sucha a way that people should understanbd the code flow.

    U have declared the array with the size of the table.But u are not using it.

    Clearly explain what u are doing and what u want to return so that we can give quick solution for u.

    -Regards
    Ramya
    Ramya:cool:

  10. #10
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    Where you are using the array "iodine_t" .You have to write the comments in sucha a way that people should understanbd the code flow.

    U have declared the array with the size of the table.But u are not using it.

    Clearly explain what u are doing and what u want to return so that we can give quick solution for u.

    -Regards
    Ramya
    Yes, i have declared tha array to be the size of the table.
    If you have noticed am using loop to do computation on each row up to the last value in the row. As stated above i want to add the result of the calculation done in each row to the array and finally return the array.
    OMG!!just tel me you understand what am doing this time round :-)

    Best Regards,
    manfizy

  11. #11
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    Java Code:
    public int iodineChange(double IFY, double MFX, double IDC, double NF){ 
    
    double equilibriumIod = equilibriumIodine(IFY,MFX,IDC);
             
    DecimalFormat myVal = new DecimalFormat("0.00E0");
     
    /*---------------------------------------------------------------------*/
    /*Formulae:  I(t+dt) = I(t) * exp(-IDC * dt) + I(eq) *                 */
    /*                  (1 - exp(-IDC * dt))                               */
    /*---------------------------------------------------------------------*/
        
    int rCount = Xenon.getRowCount();
    int arraySize = rCount;
    int[] iodine_t = new int[arraySize];//declared array size to be the same as table size
    for (int i = 0; i < rCount; i++) {    
         
    String time = (String)Xenon.getValueAt(i,0);
    if (time == null){
    break;
    }   
    
    double time_hrs  = Double.parseDouble(time);
    double time_mins = (time_hrs*60)/300;
    
    double term1 = IDC * time_mins * (-1.0);
    term1 = Math.exp(term1);
          
              
    double Iod = 1.0 - term1;
    Iod = Iod * equilibriumIod;
    
    //calculates instantaneous iodine concentration
    String pr_t = (String)Xenon.getValueAt(i, 1);
    Double powerlevel = Double.parseDouble(pr_t);
    
    double val = IFY;
    val = val * MFX;
    val = val / IDC;
    val = (val * NF * powerlevel);
    
    Iod = Iod + (term1 * val);
    
    //Add the value of Iod to the array iodine_t
    
    Iod = iodine_t[arraySize-1];
    Temp.setValueAt(myVal.format(Iod),i,2); 
    
    
     }
    System.out.println("IODINE CHANGE : "+iodine_t);
    return iodine_t[arraySize-1];
    }
    I have added afew comments in the code and i hope its understandable this time round. In short i want the to add Iod to the array and the loop repeats untill the last value in the table.

    After all this is done, i want to return the array so that i can use the result in another method.

    best regards,
    manfizy

  12. #12
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    how come my posts are not visible???

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    iam able to see the post and given reply.Just refresh the page and try once.
    Ramya:cool:

  14. #14
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Post

    Java Code:
    public int iodineChange(double IFY, double MFX, double IDC, double NF){ 
    
    double equilibriumIod = equilibriumIodine(IFY,MFX,IDC);
             
    DecimalFormat myVal = new DecimalFormat("0.00E0");
     
    /*---------------------------------------------------------------------*/
    /*Formulae:  I(t+dt) = I(t) * exp(-IDC * dt) + I(eq) *                 */
    /*                  (1 - exp(-IDC * dt))                               */
    /*---------------------------------------------------------------------*/
        
    int rCount = Xenon.getRowCount();
    int arraySize = rCount; //declare array size to be same as table size
    int[] iodine_t = new int[arraySize];
    for (int i = 0; i < rCount; i++) {    
         
    String time = (String)Xenon.getValueAt(i,0);
    if (time == null){
    break;
    }   
    
    double time_hrs  = Double.parseDouble(time);
    double time_mins = (time_hrs*60)/300;
    
    double term1 = IDC * time_mins * (-1.0);
    term1 = Math.exp(term1);
          
              
    double Iod = 1.0 - term1;
    Iod = Iod * equilibriumIod;
    
    //calculates instantaneous iodine concentration
    String pr_t = (String)Xenon.getValueAt(i, 1);
    Double powerlevel = Double.parseDouble(pr_t);
    
    double val = IFY;
    val = val * MFX;
    val = val / IDC;
    val = (val * NF * powerlevel);
    
    Iod = Iod + (term1 * val);
    
    //Add Iod to the Array
    Iod = iodine_t[arraySize-1];
    Temp.setValueAt(myVal.format(Iod),i,2); 
    
    
     }
    System.out.println("IODINE CHANGE : "+iodine_t);
    return iodine_t[arraySize-1];
    }
    I have added afew comments in the code to make it understandable.
    Inshort all i want is to add Iod to tha array iodine_t until the loop is terminated.
    After that i want to return the array

  15. #15
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    Hi,
    Go thru the modified code.
    Java Code:
    public int iodineChange(double IFY, double MFX, double IDC, double NF){ 
    
    double equilibriumIod = equilibriumIodine(IFY,MFX,IDC);
             
    DecimalFormat myVal = new DecimalFormat("0.00E0");
     
    /*---------------------------------------------------------------------*/
    /*Formulae:  I(t+dt) = I(t) * exp(-IDC * dt) + I(eq) *                 */
    /*                  (1 - exp(-IDC * dt))                               */
    /*---------------------------------------------------------------------*/
        
    int rCount = Xenon.getRowCount();
    int arraySize = rCount; //declare array size to be same as table size
    int[] iodine_t = new int[arraySize];
    for (int i = 0; i < rCount; i++) {    
         
    String time = (String)Xenon.getValueAt(i,0);
    if (time == null){
    break;
    }   
    
    double time_hrs  = Double.parseDouble(time);
    double time_mins = (time_hrs*60)/300;
    
    double term1 = IDC * time_mins * (-1.0);
    term1 = Math.exp(term1);
          
              
    double Iod = 1.0 - term1;
    Iod = Iod * equilibriumIod;
    
    //calculates instantaneous iodine concentration
    String pr_t = (String)Xenon.getValueAt(i, 1);
    Double powerlevel = Double.parseDouble(pr_t);
    
    double val = IFY;
    val = val * MFX;
    val = val / IDC;
    val = (val * NF * powerlevel);
    
    Iod = Iod + (term1 * val);
    
    [B][COLOR="SeaGreen"]//Add Iod to the Array
    //Commented this below line.
    //Iod = iodine_t[arraySize-1];
    //Added the Iod to array
    iodine_t[i] = Iod;[/COLOR][/B]
    Temp.setValueAt(myVal.format(Iod),i,2); 
    
    
     }
    [B][COLOR="SeaGreen"]//The below line prints the address only so iam commenting
    //System.out.println("IODINE CHANGE : "+iodine_t);[/COLOR][/B]
    //Added this for loop for printing array value.
    for(int j= 0; j< iodine_t.length;j++)
    {
    	System.out.println("Array  value  "+j + " ----> "+iodine_t[j]);
    }
    return iodine_t[arraySize-1];
    }
    Last edited by RamyaSivakanth; 05-25-2009 at 11:37 AM.
    Ramya:cool:

  16. #16
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    thank you very much. it worked properly
    now what i dont know is how to write the return statement.

    how can i write it???


    bets regards
    manfizy:)

  17. #17
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    842
    Rep Power
    6

    Default

    Just change the signature of the method and return statement as array object.go thru the change below.Please have a look at Javadoc for clear understanding of arrays .

    Java Code:
    [B][COLOR="DarkGreen"]public int[] iodineChange(double IFY, double MFX, double IDC, double NF)[/COLOR][/B]{ 
    
    double equilibriumIod = equilibriumIodine(IFY,MFX,IDC);
             
    DecimalFormat myVal = new DecimalFormat("0.00E0");
     
    /*---------------------------------------------------------------------*/
    /*Formulae:  I(t+dt) = I(t) * exp(-IDC * dt) + I(eq) *                 */
    /*                  (1 - exp(-IDC * dt))                               */
    /*---------------------------------------------------------------------*/
        
    int rCount = Xenon.getRowCount();
    int arraySize = rCount; //declare array size to be same as table size
    int[] iodine_t = new int[arraySize];
    for (int i = 0; i < rCount; i++) {    
         
    String time = (String)Xenon.getValueAt(i,0);
    if (time == null){
    break;
    }   
    
    double time_hrs  = Double.parseDouble(time);
    double time_mins = (time_hrs*60)/300;
    
    double term1 = IDC * time_mins * (-1.0);
    term1 = Math.exp(term1);
          
              
    double Iod = 1.0 - term1;
    Iod = Iod * equilibriumIod;
    
    //calculates instantaneous iodine concentration
    String pr_t = (String)Xenon.getValueAt(i, 1);
    Double powerlevel = Double.parseDouble(pr_t);
    
    double val = IFY;
    val = val * MFX;
    val = val / IDC;
    val = (val * NF * powerlevel);
    
    Iod = Iod + (term1 * val);
    
    //Add Iod to the Array
    //Commented this below line.
    //Iod = iodine_t[arraySize-1];
    //Added the Iod to array
    iodine_t[i] = Iod;
    
    Temp.setValueAt(myVal.format(Iod),i,2); 
    
    
     }
    //The below line prints the address only so iam commenting
    //System.out.println("IODINE CHANGE : "+iodine_t);
    for(int j= 0; j< iodine_t.length;j++)
    {
    	System.out.println("Array  value  "+j + " ----> "+iodine_t[j]);
    }
    [B][COLOR="DarkGreen"]return iodine_t;[/COLOR][/B]
    }
    Ramya:cool:

  18. #18
    Manfizy's Avatar
    Manfizy is offline Senior Member
    Join Date
    Nov 2008
    Location
    Germany
    Posts
    193
    Rep Power
    6

    Default

    thanky alot for your concern.
    Am happy now that am making a progress in my program :)

Similar Threads

  1. to retrieve multiple values from html page through jsp
    By raghu9198 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 02-17-2009, 02:43 PM
  2. accessing return values from another class
    By moaxjlou in forum New To Java
    Replies: 3
    Last Post: 10-31-2008, 02:25 AM
  3. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 04:07 AM
  4. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •