Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2003
    Thanked 0 Times in 0 Posts

    Got a question about declaring variables

    If this is part of my code:

    double costKwhr = Double.parseDouble(costKwhrField.getText());
    double consumedKwhr = Double.parseDouble(hoursPerYearField.getText());

    would I need to declare: costKwhr and hoursPerYear as doubles?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Marion, IA USA
    Thanked 83 Times in 82 Posts
    Well to answer that with a question....If you are parsing a string using the parseDouble method which returns whatever it parses as datatype of double and you want to assign that resulting data that is of datatype double to another variable what datatype should the variable probably need to be to handle data of type double?

    You already have costKwhr declared as a double. As for hoursPerYear I assume you meant hoursPerYearField then it is obviously a variable that is going to be holding a string. It looks to be part of the TextComponent class judging from the .getText() method that goes with it. Not sure why you would think that needs to be declared a double. It is actually a reference to the object you created from that TextComponent class and not a variable.
    CodingForums Supreme Overlord
    All Hail Spookster


    Posting Permissions

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