943,623 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4597
  • Java RSS
Aug 26th, 2008
0

Type mismatch: Cannot convert from void to double

Expand Post »
Hi Folks,

I'm a Java newbie so please forgive me if you've heard this one before...

Got a question for ya about an small app I am working on.

I have a banking app that creates accounts, then makes deposits and withdrawls to/from these accounts. Its very simple. I have a BankAccount class and in that class I have the following deposit method...



//Deposit method.
public void deposit(double dAmount) {

if(dAmount > 0) { // Check that deposit amount is more than zero dollars..
balance = balance + dAmount;} // Update balance with new balance.

else {JOptionPane.showMessageDialog( null, "Error: You must deposit more than 0 dollars.");}



The problem I am having is getting (retrieving) the new balance value in my tester class (with main()). I created the accounts using an ArrayList named 'accounts'. So to get the new balance I do...

//Deposits

for(int j = 0; j < totalAccounts; j++) {

String dep = JOptionPane.showInputDialog("How much would you like to deposit in this account?: ");
double deposit = Double.parseDouble(dep); //Convert string to double
double newBalance = accounts.get(j).deposit(deposit); //Call deposit method


The problem I am seeing is in the last line above. The line says go to index 'j' in the array and get the value after the deposit is made. In Eclipse the error I get is

"Type mismatch: Cannot convert from void to double."

Can anyone offer any suggestions? Should I return the balance in the deposit method instead of merely updating the balance?

I am having the same issue with the withdraw method but I suspect the solution will be the same for each. Any suggestions are greatly appreciated.

Many Thanks!

Tyster
Similar Threads
Reputation Points: 27
Solved Threads: 1
Light Poster
Tyster is offline Offline
31 posts
since Oct 2007
Aug 26th, 2008
0

Re: Type mismatch: Cannot convert from void to double

Maintain the balance in a private variable inside deposit class.

you are trying to assign a double a value of void here
double newBalance = accounts.get(j).deposit(deposit);

when you withdraw or deposit, have that update a private variable of balance

Java Syntax (Toggle Plain Text)
  1. private double balance = 0.0;
  2.  
  3. public double getBalance(){
  4. return balance;
  5. }
  6. public void withdraw(double d){
  7. //do your processing
  8.  
  9. //now set balance
  10. balance = balance - d;
  11. }
  12.  
  13.  
  14. //in your other ui code call
  15. double newBalance = accounts.get(j).getBalance();
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 26th, 2008
0

Re: Type mismatch: Cannot convert from void to double

Many Thanks! I'll give that a try.

Tyster



Maintain the balance in a private variable inside deposit class.

you are trying to assign a double a value of void here
double newBalance = accounts.get(j).deposit(deposit);

when you withdraw or deposit, have that update a private variable of balance

Java Syntax (Toggle Plain Text)
  1. private double balance = 0.0;
  2.  
  3. public double getBalance(){
  4. return balance;
  5. }
  6. public void withdraw(double d){
  7. //do your processing
  8.  
  9. //now set balance
  10. balance = balance - d;
  11. }
  12.  
  13.  
  14. //in your other ui code call
  15. double newBalance = accounts.get(j).getBalance();
Reputation Points: 27
Solved Threads: 1
Light Poster
Tyster is offline Offline
31 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: book class
Next Thread in Java Forum Timeline: Clean up my code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC