0

我正在使用 beanshell 来解析 SWIFT 数据,并且需要通过引用这些 SWIFT 标签来提取值。现在,我静态地得到这些值:

String getACRU = swiftMessage.getTagData("19A",":ACRU//");
String getANTO = swiftMessage.getTagData("19A",":ANTO//");
String getCHAR = swiftMessage.getTagData("19A",":CHAR//");
String getCOUN = swiftMessage.getTagData("19A",":COUN//");
String getEXEC = swiftMessage.getTagData("19A",":EXEC//");
String getISDI = swiftMessage.getTagData("19A",":ISDI//");
String getLADT = swiftMessage.getTagData("19A",":LADT//");
String getLEVY = swiftMessage.getTagData("19A",":LEVY//");
String getLOCL = swiftMessage.getTagData("19A",":LOCL//");
String getLOCO = swiftMessage.getTagData("19A",":LOCO//");
String getMARG = swiftMessage.getTagData("19A",":MARG//");
String getOTHR = swiftMessage.getTagData("19A",":OTHR//");
String getPOST = swiftMessage.getTagData("19A",":POST//");
String getREGF = swiftMessage.getTagData("19A",":REGF//");
String getSHIP = swiftMessage.getTagData("19A",":SHIP//");
String getSPCN = swiftMessage.getTagData("19A",":SPCN//");
String getSTAM = swiftMessage.getTagData("19A",":STAM//");
String getSTEX = swiftMessage.getTagData("19A",":STEX//");
String getTRAN = swiftMessage.getTagData("19A",":TRAN//");
String getTRAX = swiftMessage.getTagData("19A",":TRAX//");
String getVATA = swiftMessage.getTagData("19A",":VATA//");
String getWITH = swiftMessage.getTagData("19A",":WITH//");
String getCOAX = swiftMessage.getTagData("19A",":COAX//");
String getACCA = swiftMessage.getTagData("19A",":ACCA//");

我的问题有两个:优雅地重写它的最佳方法是什么,以及在 beanshell 中添加一个删除前三个字符的方法/函数的最佳方法是什么,将逗号更改为句点,一旦所有这些值都已经从消息中解析出来,将它们全部加起来?

4

2 回答 2

1

抱歉,我还是 BeanShell 和 Java 的新手,但它可以工作吗?(这就像一种解决方法......)

String [] tagArray = new String []
{ "ACRU", "ANTO", "CHAR", "COUN", "EXEC",
  "ISDI", "LADT", "LEVY", "LOCL", "LOCO",
  "MARG", "OTHR", "POST", "REGF", "SHIP",
  "SPCN", "STAM", "STEX", "TRAN", "TRAX",
  "VATA", "WITH", "COAX", "ACCA" };

for (i: tagArray) {
  // it was a test: print(i);
  eval("String get" + i + " = swiftMessage.getTagData(\"19A\", \":" + i + "//\")");
}

(对不起我的英语不好......)

于 2014-01-30T03:31:52.297 回答
0

看来这工作得很好。将我需要的所有值存储在一个数组中:

String [] tagArray = new String [] { ":ACRU//",":ANTO//",":CHAR//",":COUN//",":EXEC//",":ISDI//",":LADT//",":LEVY//",":LOCL//",":LOCO//",":MARG//",":OTHR//",":POST//",":REGF//",":SHIP//",":SPCN//",":STAM//",":STEX//",":TRAN//",":TRAX//",":VATA//",":WITH//",":COAX//",":ACCA//" };

并创建一个函数来循环和添加:

double sumTags(SwiftMessage inboundSwiftmessage, String inboundTagNumber, String [] inboundTagArray){
  double getTotal;
  for( tagArrayData : inboundTagArray ){
    String getData = stripData(inboundSwiftmessage.getTagData(inboundTagNumber,tagArrayData));
    getTotal = getTotal + Double.parseDouble(getData);
  }
  return getTotal;
}

这是删除前 3 个字符并将逗号转换为句点的函数,然后将其删除:

String stripData(String inboundString){
  if (inboundString==null){
    return "0";
  }
  else
  {
    char strippedString;
    StringBuffer strippedBuffer = new StringBuffer("");
    char [] inboundArray = inboundString.toCharArray();
    for (int counter = 3 ; counter < inboundArray.length; counter++)
    {
      strippedString = inboundArray[counter];
      strippedBuffer.append(strippedString);
    }
    return strippedBuffer.toString().replace(",",".");
  }
}
于 2012-02-01T18:36:48.137 回答