-1

In this code I keep getting:

 error: incompatible types: possible lossy conversion from long to int
        rem = num%16; 
 1 error

This is the code im working with currently.

public class BaseConverter{

public static String convertToBinary(long num){
    long binary[] = new long[40];
    int index = 0;
    while(num > 0){
        binary[index++] = num%2;
        num = num/2;
    }
    for(int i = index-1; i>= 0;i--){
        System.out.print(binary[i]);
    }
}
public static String convertToHexadecimal(long num){
    int rem;

    // For storing result
    String str=""; 

    // Digits in hexadecimal number system
    char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

    while(num>0)
    {
        rem = num%16; 
        str = hex[rem]+str; 
        num = num/16;
    }
}
}

Basically, this code is supposed to help me setup for another program which calls the static methods. But I can't compile because of this error.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
YaBoiHuni
  • 1
  • 1
  • 3

3 Answers3

0

Your problem is on the line rem = num%16; where you are assigning a value of type long to a variable of type int.

The general rule is that the result of applying one of the arithmetic operators is the larger of the two types that are being operated on. In the words of the Java Language Specification, section 15.17 ,

The type of a multiplicative expression is the promoted type of its operands.

Therefore, the expression num%16 has type long, because num has type long; even though its value will always be between -15 and 15. It's an error to assign a long expression to an int variable, without explicitly casting it.

You could write

rem = (int) num % 16;

to avoid this error.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Ok that worked! but now im getting this error: BaseConverter.java:13: error: missing return statement } ^ BaseConverter.java:30: error: missing return statement } ^ 2 errors – YaBoiHuni Jul 20 '17 at 03:59
  • Yes. That's because you're missing `return` statements in both your methods. You've said at the top of each method that they return a `String`. You need to either _make_ them return a `String`, or change `String` in each declaration to `void`. For example, adding `return str;` at the bottom of the `convertToHexadecimal` method is probably what you require. – Dawood ibn Kareem Jul 20 '17 at 04:01
  • Ok i got the return str; to work, what would i add in the first method? – YaBoiHuni Jul 20 '17 at 04:05
  • OK, you've now demonstrated that you know everything you need to know, in order to solve these problems without further input from me. Good luck. – Dawood ibn Kareem Jul 20 '17 at 04:12
0

Yes because of this code

 rem = num%16; 

rem is int type and num is long, you need to add type conversion

rem = (int)num%16;

Also add return statements for both of your methods for successful compilation.

Isha Agarwal
  • 420
  • 4
  • 12
0

use this:

rem = (int) (num % 16);

and don't forget your return statement:

return str;