| | |
Type mismatch: Cannot convert from void to double
![]() |
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
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
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
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
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
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)
private double balance = 0.0; public double getBalance(){ return balance; } public void withdraw(double d){ //do your processing //now set balance balance = balance - d; } //in your other ui code call double newBalance = accounts.get(j).getBalance();
Custom Application & Software Development
www.houseshark.net
www.houseshark.net
•
•
Join Date: Oct 2007
Posts: 25
Reputation:
Solved Threads: 1
Many Thanks! I'll give that a try.
Tyster
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)
private double balance = 0.0; public double getBalance(){ return balance; } public void withdraw(double d){ //do your processing //now set balance balance = balance - d; } //in your other ui code call double newBalance = accounts.get(j).getBalance();
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: book class
- Next Thread: Clean up my code
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






