why don't you use what java is providing you instead of re-inventing the wheel:
int n = 27;
String str = Integer.toString(n, 16);
System.out.println(str);
You can also use Integer.toHexString(n)
instead of Integer.toString(n, 16)
.
And if you still want to do it yourself, here is the corrected version of your code:
int n = 27;
int m = n;
int r = m % 16;
String sum = "";
while (m!=0) {
r = m % 16;
m = m / 16;
switch (r) {
case 10:
sum = "A" + sum;
break;
case 11:
sum = "B" + sum;
break;
case 12:
sum = "C" + sum;
break;
case 13:
sum = "D" + sum;
break;
case 14:
sum = "E" + sum;
break;
case 15:
sum = "F" + sum;
break;
default:
sum = r + sum;
}
}
System.out.print(sum);