Is it possible to have multiple return values in the same method???
Printable View
Is it possible to have multiple return values in the same method???
Directly, no. But you can return an array which can hold as many return values as you you want.
Luck,
CJSL
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
Hi,
Create an instance and call the method.
-Regards
Ramya
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:
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
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.
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.
gets the number of rows in the table xenon.Code:int rCount = Xenon.getRowCount();
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
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
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.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];
}
After all this is done, i want to return the array so that i can use the result in another method.
best regards,
manfizy
how come my posts are not visible???
iam able to see the post and given reply.Just refresh the page and try once.
I have added afew comments in the code to make it understandable.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];
}
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
Hi,
Go thru the modified code.
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];
}
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:)
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 .
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]
}
thanky alot for your concern.
Am happy now that am making a progress in my program :)