Reputation: 9
I have spent a few hours writing a code for my final programmation project, and after researching and looking my code a lot of times I cant figure out where the problem is. The application simulates a bank account where I have a name and a bank account number, the problem comes when I try to check that the bank account number is correct, it uses a weird algorithm that bank uses here in my country to check if a bank account number is correct and I have made a method to check it myself, here it is:
private static boolean esCorrecto(String numero) {
if(numero.length() != 20){
return false;
}else{
String digCtrl = numero.substring(8, 9);
int digCtrlV = (Integer.parseInt(numero.substring(0,0)) * 4) + (Integer.parseInt(numero.substring(1,1)) * 8) +
(Integer.parseInt(numero.substring(2,2)) * 5) + (Integer.parseInt(numero.substring(3,3)) * 10) +
(Integer.parseInt(numero.substring(4,4)) * 9) + (Integer.parseInt(numero.substring(5,5)) * 7) +
(Integer.parseInt(numero.substring(6,6)) * 3) + (Integer.parseInt(numero.substring(7,7)) * 6);
digCtrlV = digCtrlV%11;
digCtrlV = 11 - digCtrlV;
if(digCtrlV == 10){
digCtrlV = 1;
}
int digCtrlV1 = (Integer.parseInt(numero.substring(10,10)) * 4) + (Integer.parseInt(numero.substring(11,11)) * 4) +
(Integer.parseInt(numero.substring(12,12)) * 4) + (Integer.parseInt(numero.substring(13,13)) * 8) +
(Integer.parseInt(numero.substring(14,14)) * 5) + (Integer.parseInt(numero.substring(15,15)) * 10) +
(Integer.parseInt(numero.substring(16,16)) * 9) + (Integer.parseInt(numero.substring(17,17)) * 7) +
(Integer.parseInt(numero.substring(18,18)) * 3) + (Integer.parseInt(numero.substring(19,19)) * 6);
digCtrlV1 = digCtrlV1%11;
digCtrlV1 = 11 - digCtrlV1;
if(digCtrlV1 == 10){
digCtrlV1 = 1;
}
String digCtrlVerdaderos = Integer.toString(digCtrlV) + Integer.toString(digCtrlV1);
if(digCtrl.equals(digCtrlVerdaderos)){
return true;
}else
return false;
}
First it checks if the length of the number is correct (20 digit) and then it checks if the control numbers (digit 9 and 10) are correct by doing some operations. Main method has several lines but Im getting error in the following part:
public static void main(String[] args) throws Ex {
int control = -1;
Scanner teclado = new Scanner(System.in);
System.out.println("Introduce nombre del titular y un numero de cuenta (pulsar enter despues de cada introduccion) ");
CuentaBancaria cta = new CuentaBancaria(teclado.next(), teclado.next(), 0);
if(esCorrecto(cta.getNumeroCuenta())){
Finally the error Im getting is this one:
Exception in thread "main" java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at java.lang.Integer.parseInt(Integer.java:615) at proyectofinal.ProyectoFinal.esCorrecto(ProyectoFinal.java:82) at proyectofinal.ProyectoFinal.main(ProyectoFinal.java:21) Java Result: 1
Upvotes: 0
Views: 1171
Reputation: 50061
numero.substring(n, n)
is a zero-length string. To extract a one-character string you want numero.substring(n, n+1)
. See String.substring
doc.
Instead of making substrings, you could also use Character.digit
; although that returns -1 for invalid input rather than throwing an exception, so to catch errors (and for neatness) it would be better to wrap it into a method:
private static int digit(String s, int pos) {
int digit = Character.digit(s.charAt(pos), 10);
if (digit == -1) throw new NumberFormatException(s);
return digit;
}
Upvotes: 3
Reputation: 6248
You keep using substring with equal start and end, and substring extracts and empty string ""
. Use substring(i, i+1)
in order to get a digit on the position i
.
Upvotes: 0