Results 1 to 4 of 4
  1. #1
    silvia is offline Member
    Join Date
    Jul 2007
    Posts
    35
    Rep Power
    0

    Default Error: unexpected type

    Hi, I have tried every way I could think to correct this problem. This is the last of 13 errors I had to find debugging this assignment.

    Java Code:
    interest = annualInterest 12;
    after compiling it, the error message said:

    Java Code:
    possible loss of precision
    
    found: double
    
    required: int
    the declaring variables are

    int price, downPayment, tradeIn, months, loanAmt, interest;
    double annualInterest, payment;

    under conversions there is this:

    Java Code:
    annualInterest =Double.parseDouble(inputAnnualInterest);
    under calculations the line of code is
    Java Code:
    interest = annualInterest 12;
    I tried putting "/" in front of 12, got an error. Tried putting parantheses around
    (annualInterest 12) got an error. How to get int when there is a double. I determined from declared variable annualInterest was the double. So here is what I tried next:

    Java Code:
    interest = (double = (annualInterest / 12));
    got an error back
    Java Code:
    unexpected type
    required: variable
    found : class
    interest = (double = (annualInterest / 12));
    ^(under the d in double)
    1 error

    My understanding is everything in the ( ) has to equate to an int and not double...since interest is an int. Must be close because the error message no longer says found: double required: int. It just says found:class required variable.

    Maybe someone can clarify my understanding.

    Thanks

  2. #2
    barney is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default

    Java Code:
    interest = annualInterest / 12;
    Think to yourself what are you DOING with interest. How do you get it. You weren't really doing anything to it. The interest might be better off as a double. In the future post all of your code.
    Greetings.

  3. #3
    trestles28 is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    This is called type casting, and if interest is of type double it's:
    interest = (double)(annualInterest/12);

  4. #4
    FlyNn is offline Senior Member
    Join Date
    Feb 2010
    Posts
    128
    Rep Power
    0

    Default

    agreed with barney. If i am correct int uses 32 bits. Double uses double-precision 64 bits. You cant put a 64 bit value into 32, so the compiler shouts at you. Double is the largest datatype used by java.
    This can give you more information if needed: Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Good luck

Similar Threads

  1. Replies: 3
    Last Post: 06-05-2010, 08:05 PM
  2. Putting your own type in a Set
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:32 PM
  3. Type Casting Help
    By rhm54 in forum New To Java
    Replies: 2
    Last Post: 02-07-2008, 12:06 PM
  4. An unexpected jumper bug on my IO code?
    By cruxblack in forum New To Java
    Replies: 7
    Last Post: 07-29-2007, 08:24 AM
  5. The return type
    By Marcus in forum New To Java
    Replies: 1
    Last Post: 07-05-2007, 06:28 AM

Posting Permissions

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