Results 1 to 2 of 2
  1. #1
    Vikus is offline Member
    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Question 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.)
    http://i.imgur.com/cQdmddq.png
    http://i.imgur.com/y4eGUyk.png
    /irrelevant

    Relevant stuff:

    AccountTest.java:
    Java Code:
    import java.util.Scanner; // No longer being used because the purpose of the code changed.
    import javax.swing.JOptionPane;
    
    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. */
    
    
    		JOptionPane.showMessageDialog(null, message);
    
    		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.
    		JOptionPane.showMessageDialog(null, message2);
    		account2.Credit( depositAmount ); // Credit function of Account (See Account.java)
    
    		JOptionPane.showMessageDialog(null, message3);
    
    	}
    }
    Account.java:
    Java Code:
    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()
    	{
    		return balance;
    	}
    }
    If I missed something that needs to be included, let me know and I'll put it in.
    Last edited by Vikus; 04-05-2013 at 05:51 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,328
    Rep Power
    5

    Default 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.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Compile Error
    By StevenR in forum New To Java
    Replies: 2
    Last Post: 02-20-2013, 09:48 AM
  2. compile error
    By missmara77 in forum New To Java
    Replies: 1
    Last Post: 10-31-2011, 07:48 PM
  3. Compile Error
    By gcorreageek in forum Advanced Java
    Replies: 2
    Last Post: 09-08-2010, 05:23 AM
  4. Compile Error - Please Help!!
    By AJ2009 in forum New To Java
    Replies: 10
    Last Post: 01-04-2009, 03:59 PM
  5. compile error
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-12-2007, 06:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •