First, I think you missed one *
before the peso++
, which, by the way, 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 operators ++
and --
in their own isolated instructions. Otherwise it gets very confusing. How they are incremented, decremented before being used in soma += ...
, then they would stay before the soma += ...
:
while (qtos)
{
qtos--;
peso++;
soma += (*(num + qtos) - '0') * peso;
}
In place of while( qtos )
just use while (qtos >= 0)
.
I think you wanted num
were a String
and not a char
. If in its original code, char num
were char *num
, certainly that would be it. If that were the case, the *(num + qtos)
would become in Java num.charAt(qtos)
. In fact, the variable qtos
is the size of String
, and therefore does not need to be passed as a parameter and should be set with the initial value of num.length()
.
This can be simplified from 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 it is possible to do this:
remainder = 11 - (soma % 11);
return (char) ('0' + (remainder >= 10 ? 0 : remainder));
It’s also a good idea to declare the variable only when you need it.
Your code would look 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 remarks:
I have doubts if it’s best to return char
or return int
, I think that 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 into a loop for
.
It doesn’t 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.
It is possible to see that there is a relationship between the size of num
, peso
and qtos
. The value of qtos
begins in num.length() - 1
and decreases to zero. The value of peso
goes from 1 until num.length()
. In fact, in your code, when one increases the other decreases. With this, we can use the qtos == num.length() - peso
and then delete the variable peso
and we can also iterate the characters of num
in any order.
If we iterate the characters of num
in the order they appear, we can use the syntax of Enhanced-for.
With that, the code would be:
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;
}
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero