Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting a return value from a function and using it another fuction

 
Stephen Adams
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a simple function that checks a value passed in and returns a value depending on the original value, like this:
public void setBalance(double balance){
if(balance >= 0){
this.balance=balance;
return true;
}
else{
return false;
}
}
Then I want to use the result of this function in another function, that displays a message to the user, e.g.
public void isBalanceNegative(double balance,){
if(){
System.out.println("You cannot enter a negative number");
}
else{
System.out.println("Your current balance is " + balance);
}
}
The two problems I am having are, one how to pass the returned true or false value from the setBalance function to the isBalanceNegative function and how to get the balance to the isBalanceNegative function too.
I suppose the main question is how to pass the results of one function to another?
THanks
Stephen
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you must set a return type
[CODE]
public boolean setBalance(double balance){
if(balance >= 0){
this.balance=balance;
return true;
}else{
return false;
}
}
[CODE]
and then call your method like
[CODE]
booelan result = setBalance()
[CODE]
this value you can then easily check.
 
Stephen Adams
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool, thanks Oliver.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic