First, I think you forgot a *
before peso++
, which I think should be ++peso
in your case.
The variables soma
and peso
should be initialized. Their initial value is zero.
I suggest separating the use of the ++
and --
operators into their own isolated statements. Otherwise it gets very confusing. As they are incremented, decremented before being used in soma += ...
, then they would be before soma += ...
:
while (qtos)
{
qtos--;
peso++;
soma += (*(num + qtos) - '0') * peso;
}
Instead of while( qtos )
just use while (qtos >= 0)
.
I think you wanted num
to be String
and not char
. If in your original code, char num
was char *num
, that would be it. If this is the case, *(num + qtos)
would become Java num.charAt(qtos)
. In fact, the variable qtos
is the size of String
, so it does not need to be passed as a parameter, and should be set to the initial value of num.length()
.
This can be simplified here:
remainder = soma % 11;
return ((char) ((remainder == 0 || remainder == 1) ? '0' : 59 - remainder));
The (remainder == 0 || remainder == 1)
can be simplified to remainder <= 1
:
remainder = soma % 11;
return ((char) (remainder <= 1 ? '0' : 59 - remainder));
The idea is to return 11 - remainder
, except in cases where this would result in 10 or 11. So you can do this:
remainder = 11 - (soma % 11);
return (char) ('0' + (remainder >= 10 ? 0 : remainder));
It is also a good idea to declare the variable only when you need it.
Your code looks like this:
char metodoDigito(String num) {
short soma = 0, peso = 0, qtos = num.length();
while (qtos >= 0) {
qtos--;
peso++;
soma += (num.charAt(qtos) - '0') * peso;
}
short remainder = 11 - (soma % 11);
return (char) ('0' + (remainder >= 10 ? 0 : remainder));
}
Three more comments:
-
I am wondering if it is best to return char
or return int
, I think int
would be better.
-
There is also little reason to use short
in Java since the default integer type in Java is int
.
-
Your while
can also be converted to a for
loop.
-
It does not make much sense to declare the name metodo
in a method. It is totally redundant and unnecessary. The name digitoVerificador
(or even just dv
) would be better.
-
Probably this method would be static and public because it does not use instances of the class where it is inserted and does not appear to be an internal functionality of some class.
-
You can see that there is a relationship between the size of num
, peso
and qtos
. The qtos
value starts at num.length() - 1
and decreases to zero. The peso
value goes from 1 to num.length()
. In fact, in your code, when one increases the other decreases. With this, we can use the qtos == num.length() - peso
relation and then delete the variable peso
and we can also iterate the characters of num
in any order.
-
-
-
-
With this, the code would be this:
public static int digitoVerificador(String num) {
int soma = 0, peso = 0;
for (char c : num.toCharArray()) {
peso++;
soma += (c - '0') * peso;
}
int remainder = 11 - (soma % 11);
return remainder >= 10 ? 0 : remainder;
}