Vote count:
0
I have a method which converts string to a MeterNumber. The code is as follows.
public static MeterNumber fromString(String number) {
MeterNumber mn = new MeterNumber(false);
int i = 0, // position in number
n = 0, // number of digits
len = number.length();
boolean valid = true;
while (valid && i < len) {
char c = number.charAt(i);
i = i + 1;
if (n < 9 && Character.isDigit(c)) {
mn.digits[n] = c - '0';
n = n + 1;
} else if (Character.isWhitespace(c)) {
// skip spaces
} else {
valid = false;
}
}
int cs = mn.checksum();
if (valid && i == len && n == 9 &&
cs / 10 == mn.digits[7] && cs % 10 == mn.digits[8]) {
return mn;
} else {
return null;
}
}
It's suppose to read an input like this:
531 481 889 O 788.5
652 364 795 P 2442.7
It will work fine if I just provide:
531 481 889
652 364 795
However it returns null if my input is in this format:
531 481 889 O 788.5
652 364 795 P 2442.7
Can you please help me see the problem. Why is the Char and double at the end of the line interfering?
Thanks in advance.
asked 50 secs ago
fromString() method that doesn't convert properly
Aucun commentaire:
Enregistrer un commentaire