Reputation:
i have this method :
public static String appliquerCoup( String combinaison, String coup ){
String res = "";
for(int i = 0; i < 3; i++){
res = res + (combinaison.charAt(i) - coup.charAt(i));
}
return res;
}
if i apply this in the main
System.out.println(appliquerCoup("414", "020")
i have :
4-14
As logical as it is, it's puting me a -1
for 2-1 = -1
I want it to put me 0 if the operation is inferior to 0.
So i made these changes :
public static String appliquerCoup( String combinaison, String coup ){
String res = "";
for(int i = 0; i < 3; i++){
char cFinal = combinaison.charAt(i) - coup.charAt(i);
if(cFinal < '0')
cFinal = 0;
res = res + cFinal;
}
return res;
}
but it is saying me that on the 4th line that there's
possible loss of precision
found :int; required: char
Can you help me find the solution ? It's been a long i'm trying.
Thanks so much
Upvotes: 1
Views: 117
Reputation: 59112
The result of subtracting one character from another is an integer. It tells you the distance between them.
Imagine you were calculating 'b' - 'a'
. The difference between them is the number 1
, not the character '1'
.
You can change cFinal
to an int
and it should work, since it's legal to append integers to a string:
int cFinal = combinaison.charAt(i) - coup.charAt(i);
if (cFinal < 0) // NB: compare with 0, not '0'
cFinal = 0;
res = res + cFinal;
Or you can use max
to enforce the lower bound more succinctly.
res += Math.max(0, combinaison.charAt(i) - coup.charAt(i));
Upvotes: 1