Odd compile error with my double?
EDIT: I added a value to my initialization of depositAmount and the compile error went away. Shouldn't it automatically initialize to 0.0 without me needing to state that? Problem kinda solved, otherwise.
EDIT2: New issue. Now Integer.parseInt (Double.parseDouble(?) aren't carrying over the string gathered from the input dialog. I need a moment to update the code.
EDIT3: The code has been updated to fit what I currently have. If anyone can help with the parsing issue, it is much appreciated.
EDIT4: On a side note, using Double.parseDouble() does not give me a compile error. The code runs, but it won't carry the number over?
(This information is now irrelevant)
I'm following the guide of a book to learn Java, and I'm doing a bonus part where I learn some GUI. It's very basic (only stuff with JOptionPane, input/showmessagedialog). The thing I'm trying to accomplish is making a simplified AccountTest display messages in the JOptionPane boxes, but it seems one of my strings no longer wants to use my double.
I'll include code and a screenshot of the compile issue, since what I'm saying feels like it might not be making much sense.
Before coming to the forum, I tried moving the double around, I completely rewrote the strings to see if it had any difference, and reorganized the JOptionPane stuff at the bottom. I have no idea what's causing the error due to my lack of knowledge in Java, so any help is appreciated.
The compile issue is at the bottom. (The second link was the first issue, but I had a spontaneous idea that only procured another compile issue.)
import java.util.Scanner; // No longer being used because the purpose of the code changed.
public class AccountTest
public static void main( String args )
Account account2 = new Account(10.00); // New object of Account, initialized with a double
double depositAmount = 0.0;
String message = "The balance of account2 is: " + account2.getBalance();
String message2 = String.format("Depositing %.2f to account2.", depositAmount);
String message3 = "The new balance for account2 is " + account2.getBalance();
/* EDIT: This is now working fine. */
String dpam = JOptionPane.showInputDialog("Enter deposit amount for account2: ");
// dpam = depositamount because I already used the name once.
depositAmount = Double.parseDouble(dpam);
// This is my first time using Integer.parseInt. It might be incorrect. Please correct me if it is.
// EDIT: Shouldn't this be Double.parseDouble? The book didn't say so.
// EDIT2: I edited this to use Double.parseDouble, and it still doesn't work.
account2.Credit( depositAmount ); // Credit function of Account (See Account.java)
If I missed something that needs to be included, let me know and I'll put it in.
public class Account
private double balance;
public Account( double initialBalance )
if (initialBalance > 0.0 )
balance = initialBalance;
public void Credit( double amount ) // Adds amount to balance
balance = balance + amount;
public void Discredit( double amount ) // Removes amount from balance
balance = balance - amount;
public double getBalance()
Re: Odd compile error with my double?
First, only instance variables have default values. Local variables like depositAmount do not.
You can convert an int to a double without a cast. But not a double to an int.
And the big problem is that you are building your strings before you call your OptionPanes. And part of those strings include the calls to the account methods which have not been updated yet. So you are actually printing the amount that was there before the desposit was made.