Results 1 to 4 of 4
  1. #1
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Incompatible type conversions

    Java Code:
      public int iValue(int iLength, int iNumber) {
             
             int flag = 1;
    
             for (int i = 1; i < iLength; i-- ) {
                 
                 iValue = iNumber%Math.pow(10,iLength) ;
                 iValue = iNumber - iValue ;          
                 iValue = iValue/Math.pow(10,i-1);
                 return iValue;
                 iSum(iValue, i, iLength, flag);    
        
             } // end of for
             
      }
    The error messages I get (this is just a snippet of the code) are

    Java Code:
    HelloMath.java:15:30: error: incompatible types: possible lossy conversion from double to int
                 iValue = iNumber%Math.pow(10,iLength) ;
                                 ^
    HelloMath.java:17:29: error: incompatible types: possible lossy conversion from double to int
                 iValue = iValue/Math.pow(10,i-1);
    I have tried renaming variables in a new type (double or long) but that does not work.
    I can work around this by doing some very ugly coding to get around the Math.pow limitation.
    Any suggestions?

    THanks...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Incompatible type conversions

    Use cast to int for the double value of the expression.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Incompatible type conversions

    And a couple of other things.

    1. Line 11 won't be reached.
    2. Probably not a good idea to name a variable and method the same
    3. You didn't declare iValue as a local (based on code I am assuming its not an instance field)

    And finally, what exactly is this supposed to do? (just curious)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    TeacherZ is offline Member
    Join Date
    Feb 2017
    Posts
    24
    Rep Power
    0

    Default Re: Incompatible type conversions

    Norm:

    Thanks, I misunderstood the documentation of casting to int. My apologies for a silly question and thanks for the kind response.

    Jim:

    As per your suggestion, I am trying to do an Armstrong number calculation.
    This code snippet is merely part of that effort.
    I knew this code block was faulty as I am unclear on return values and getting values from an instance of the class into the main.
    I probably should have prefaced everything by saying I knew this was ultimately faulty.

    But I want to gnaw on return values a bit more before I ask any questions.

    Thanks for your comments... It is really good to be forewarned like this.
    I am sure I will have other issues, but I do try to understand my errors before admitting defeat.
    Really do appreciate the kind answers.

    Mark

Similar Threads

  1. incompatible type
    By atiqahazmi in forum New To Java
    Replies: 16
    Last Post: 04-10-2014, 09:47 PM
  2. Incompatible type
    By SuitedSlime in forum New To Java
    Replies: 2
    Last Post: 09-15-2013, 08:02 PM
  3. Why am I getting the error: Incompatible Type
    By bigsonny in forum New To Java
    Replies: 4
    Last Post: 07-31-2011, 02:41 AM
  4. Replies: 1
    Last Post: 07-27-2010, 06:01 PM
  5. Help in incompatible type JTextArea
    By sj_420 in forum Advanced Java
    Replies: 4
    Last Post: 04-15-2010, 03:00 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
  •