I am working on java IBAN Validator that should take IBAN numbers from particular Excel column and validate them with output "valid/invalid".
I have the validator itself set up in IBANChecker03.java class. It works just fine when I put any number of IBAN numbers into String[] ibans = {"iban number placed here"}
In my second class, temporarily called "HomeOffice.java", I have all the handeling of the Excel file. It iterates through the raws and takes all the IBANs for me + stores it into "cellvalue" variable.
Now I need to kind of connect these two classes together, which is what I am currently stuck on.
May you please help me how can I now take results from cellvalue
and pass it into IBANChecker03.java class in String[] ibans = {""}
so that it validates the ibans for me?
What would be the best thing of doing this?
Here is the code:
public class IBANChecker03
{
private static final String DEFSTRS = ""
+ "AL28 AD24 AT20 AZ28 BE16 BH22 BA20 BR29 BG22 "
+ "HR21 CY28 CZ24 DK18 DO28 EE20 FO18 FI18 FR27 GE22 DE22 GI23 "
+ "GL18 GT28 HU28 IS26 IE22 IL23 IT27 KZ20 KW30 LV21 LB28 LI21 "
+ "LT20 LU20 MK19 MT31 MR27 MU30 MC27 MD24 ME22 NL18 NO15 PK24 "
+ "PS29 PL28 PT25 RO24 SM27 SA24 RS22 SK24 SI19 ES24 SE24 CH21 "
+ "TN24 TR26 AE23 GB22 VG24 GR27 CR21";
private static final Map<String, Integer> DEFINITIONS = new HashMap<>();
static
{
for (String definition : DEFSTRS.split(""))
DEFINITIONS.put(definition.substring(0, 2), Integer.parseInt(definition.substring(2)));
}
public static void main(String[] args)
throws FileNotFoundException, IOException, Exception
{
String[] ibans = {"IBAN goes here"};
// HERE I NEED TO PASS Cell Values from HomeOffice.java
for (String iban : ibans)
System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");
}
static boolean validateIBAN(String iban)
{
iban = iban.replaceAll("\\s", "").toUpperCase(Locale.ROOT);
int len = iban.length();
if (len < 4 || !iban.matches("[0-9A-Z]+") || DEFINITIONS.getOrDefault(iban.substring(0, 2), 0) != len)
return false;
iban = iban.substring(4) + iban.substring(0, 4);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
sb.append(Character.digit(iban.charAt(i), 36));
BigInteger bigInt = new BigInteger(sb.toString());
return bigInt.mod(BigInteger.valueOf(97)).intValue() == 1;
}
}
public class HomeOffice
{
public static void main(String[] args)
throws Exception
{
File excelFile = new File("IBNTEST.xlsx");
FileInputStream fis = new FileInputStream(excelFile);
XSSFWorkbook workbook = new XSSFWorkbook(fis);
XSSFSheet sheet = workbook.getSheetAt(0);
// Here we start iterating through raws and cells
Iterator<Row> rowIt=sheet.iterator();
while (rowIt.hasNext()) {
Row row = rowIt.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getColumnIndex() == 7) { // Choose number of column
System.out.println(cell.toString() + ","); // Print cells
String cellvalue = cell.toString(); // This I need to pass into IBANCHECKER03.java
// Pass into String[] ibans = {};
//-----------HERE I NEED TO CONNECT TO IBANChecker
}
workbook.close();
fis.close();
}
}
}
}