java—在两个主类之间传递字符串变量,并将其传递到字符串数组中

evrscar2  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(591)

我正在开发javaiban验证器,它应该从特定的excel列中获取iban编号,并用输出“valid/invalid”验证它们。
我在ibanchecker03.java类中设置了验证器本身。当我把任意数量的iban数字放入 String[] ibans = {"iban number placed here"} 在我的第二节课中,临时称为“homeoffice.java”,我掌握了excel文件的所有操作。它遍历raw并获取所有iban for me+并将其存储到“cellvalue”变量中。
现在我需要把这两个类连接起来,这就是我目前所坚持的。
你能帮我怎样才能从中得到结果吗 cellvalue 并将其传递到中的ibanchecker03.java类中 String[] ibans = {""} 所以它能帮我验证IBAN?
这样做最好的办法是什么?
代码如下:

  1. public class IBANChecker03
  2. {
  3. private static final String DEFSTRS = ""
  4. + "AL28 AD24 AT20 AZ28 BE16 BH22 BA20 BR29 BG22 "
  5. + "HR21 CY28 CZ24 DK18 DO28 EE20 FO18 FI18 FR27 GE22 DE22 GI23 "
  6. + "GL18 GT28 HU28 IS26 IE22 IL23 IT27 KZ20 KW30 LV21 LB28 LI21 "
  7. + "LT20 LU20 MK19 MT31 MR27 MU30 MC27 MD24 ME22 NL18 NO15 PK24 "
  8. + "PS29 PL28 PT25 RO24 SM27 SA24 RS22 SK24 SI19 ES24 SE24 CH21 "
  9. + "TN24 TR26 AE23 GB22 VG24 GR27 CR21";
  10. private static final Map<String, Integer> DEFINITIONS = new HashMap<>();
  11. static
  12. {
  13. for (String definition : DEFSTRS.split(" "))
  14. DEFINITIONS.put(definition.substring(0, 2), Integer.parseInt(definition.substring(2)));
  15. }
  16. public static void main(String[] args)
  17. throws FileNotFoundException, IOException, Exception
  18. {
  19. String[] ibans = {"IBAN goes here"};
  20. // HERE I NEED TO PASS Cell Values from HomeOffice.java
  21. for (String iban : ibans)
  22. System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");
  23. }
  24. static boolean validateIBAN(String iban)
  25. {
  26. iban = iban.replaceAll("\\s", "").toUpperCase(Locale.ROOT);
  27. int len = iban.length();
  28. if (len < 4 || !iban.matches("[0-9A-Z]+") || DEFINITIONS.getOrDefault(iban.substring(0, 2), 0) != len)
  29. return false;
  30. iban = iban.substring(4) + iban.substring(0, 4);
  31. StringBuilder sb = new StringBuilder();
  32. for (int i = 0; i < len; i++)
  33. sb.append(Character.digit(iban.charAt(i), 36));
  34. BigInteger bigInt = new BigInteger(sb.toString());
  35. return bigInt.mod(BigInteger.valueOf(97)).intValue() == 1;
  36. }
  37. }
  38. public class HomeOffice
  39. {
  40. public static void main(String[] args)
  41. throws Exception
  42. {
  43. File excelFile = new File("IBNTEST.xlsx");
  44. FileInputStream fis = new FileInputStream(excelFile);
  45. XSSFWorkbook workbook = new XSSFWorkbook(fis);
  46. XSSFSheet sheet = workbook.getSheetAt(0);
  47. // Here we start iterating through raws and cells
  48. Iterator<Row> rowIt=sheet.iterator();
  49. while (rowIt.hasNext()) {
  50. Row row = rowIt.next();
  51. Iterator<Cell> cellIterator = row.cellIterator();
  52. while (cellIterator.hasNext()) {
  53. Cell cell = cellIterator.next();
  54. if (cell.getColumnIndex() == 7) { // Choose number of column
  55. System.out.println(cell.toString() + ","); // Print cells
  56. String cellvalue = cell.toString(); // This I need to pass into IBANCHECKER03.java
  57. // Pass into String[] ibans = {};
  58. //-----------HERE I NEED TO CONNECT TO IBANChecker
  59. }
  60. workbook.close();
  61. fis.close();
  62. }
  63. }
  64. }
  65. }
nwnhqdif

nwnhqdif1#

如果你使用 args 传递给 IBANChecker03.main 作为要检查的字符串数组,您可以使用 HomeOffice 像这样:

  1. public class IBANChecker03 {
  2. public static void main(String[] args) {
  3. String[] ibans = args;
  4. for(String iban : ibans){
  5. System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");
  6. }
  7. }
  8. static boolean validateIBAN(String iban){
  9. // validation code
  10. }
  11. }
  12. public class HomeOffice {
  13. public static void main(String[] args) {
  14. String[] vals = new String[]{"5616516", "8984546", "6516546", "9684654"};
  15. IBANChecker03.main(vals);
  16. }
  17. }

但是,在您提供的代码中,您正在遍历每个单元格,并且您已经指出希望将单个单元格的值传递给 IBANChecker03 . 此值:

  1. String cellvalue = cell.toString();

你可以用很多方法来处理这个问题。可以将单元格值打包到数组中并传递:

  1. String[] cellvalue = new String[]{cell.toString()};
  2. IBANChecker03.main(cellValue);

你可以让 validateIBAN public,或者如果这些to类在同一个包中,那么您可以直接调用它,因为默认情况下方法是包私有的:

  1. String cellvalue = cell.toString();
  2. if(IBANChecker03.validateIBAN(cellvalue)){
  3. //Do Something
  4. }

如果你真正感兴趣的是 IBANChecker03 验证并打印单个单元格,但您希望 main 方法,然后把逻辑从 main 像这样:

  1. public class IBANChecker03 {
  2. public static void printValid(String iban){
  3. System.out.printf("%s is %s.%n", iban, validateIBAN(iban) ? "valid" : "not valid");
  4. }
  5. public static void main(String[] args) {
  6. String[] ibans = {"IBAN goes here"};
  7. for(String iban : ibans){
  8. printValid(iban);
  9. }
  10. }
  11. static boolean validateIBAN(String iban){
  12. //Validation code
  13. }
  14. }
  15. public class HomeOffice {
  16. public static void main(String[] args) {
  17. //Code to get cell
  18. String cellvalue = cell.toString();
  19. IBANChecker03.printValid(cellvalue);
  20. }
  21. }
展开查看全部

相关问题