import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String iban = "";
do {
try {
System.out.println("Calcular IBAN de 24 dígitos:");
iban = br.readLine().toUpperCase();
if (iban.equals("FIN")) {
System.out.println("Fin del programa.");
} else {
if (verificar(iban)) {
System.out.println("Ok");
} else {
System.out.println("Error");
}
}
} catch (IOException e) {
e.printStackTrace();
}
} while (!iban.equals("FIN"));
}
private static boolean verificar(String iban) {
//trim no acorta los espacios entre palabras
iban = iban.replace(" ", "");
// si tiene los 2 iniciales en letras;
if (isNotNumeric(iban.charAt(0)) && isNotNumeric(iban.charAt(1)) && iban.length() > 4) {
iban = iban.replace(iban.substring(0, 4), "") + iban.substring(0, 4);
for (int x = 0; x < iban.length(); x++) {
char l = iban.charAt(x);
if (isNotNumeric(l)) {
iban = iban.replace(String.valueOf(l), String.valueOf(calcLetra(l)));
}
}
return comprobar(iban).equalsIgnoreCase("1");
} else {
return false;
}
}
private static String comprobar(String iban) {
if (iban.length() >= 9) {
int calcular = Integer.parseInt(iban.substring(0, 9));
int resto = calcular % 97;
String restoIban = resto + iban.substring(9);
return comprobar(restoIban);
} else {
int sobra = Integer.parseInt(iban);
return String.valueOf((sobra % 97));
}
}
private static int calcLetra(char v) {
return v - 55;
}
private static boolean isNotNumeric(char dato) {
try {
Integer.parseInt(String.valueOf(dato));
return false;
} catch (NumberFormatException e) {
return true;
}
}
}
[/aux_code]
Verificar IBAN
con
No hay comentarios
[aux_code language=»javascript» theme=»tomorrow» title=»Verificar IBAN» extra_classes=»»]