Domanda

I have below strings:

String str1 = "$123.00";
String str2 = "$(123.05)";
String str3 = "incorrectString";

I want to check and output like:

if(str1 is a valid currency){
     System.out.println("Str1 Valid");
}else{
      System.out.println("Str1 InValid");
}

if(str2 is a valid currency){
     System.out.println("Str2 Valid");
}else{
      System.out.println("Str2 InValid");
}

if(str3 is a valid currency){
     System.out.println("Str3 Valid");
}else{
      System.out.println("Str3 InValid");
}

UseCase: I am parsing a pdf using pdfbox. Given a searchterm say "abc", I want to read next token after the search term. For this purpose, I am searching for the search term in parsed pdf text and then reading the next token to that search term.

The token should be a valid currency. But there could be a case where in "abc" is present at two different places in a page with one having valid currency token next to it while the other not.

So I want to put in a check that if the token I am reading is not a valid currency token, break the loop and continue the search on the page.

I did it as below:

if (tokenRead.length() > 0) {
   String temp = tokenRead.replace("$", "").replaceAll("\\(", "");
   char checkFirstChar = temp.trim().charAt(0);
   if (!(checkFirstChar >= '0' && checkFirstChar <= '9')) {
          System.out.println("breaking");
          break;
    }
 }

This works, but I believe there should be a elegant solution using NumberFormat. Hence the question!

Thanks for reading!

È stato utile?

Soluzione

NumberFormat has nothing out of the box for your use case.

A possible solution I could come up with is this:

Currency currency = Currency.getInstance(currentLocale);
String symbol = currency.getSymbol();
if(string.startsWith(symbol) || string.endsWith(symbol)){
    System.out.println("valid");
}else{
    System.out.println("invalid");
}

But then you still need to check if the rest of the string can be parsed to a number.

Therefore I recommend to have a look at Apache Commons Currency Validator, it may fit your needs:

@Test
public void test() {
    BigDecimalValidator validator = CurrencyValidator.getInstance();

    BigDecimal amount = validator.validate("$123.00", Locale.US);
    assertNotNull(amount);

    //remove the brackets since this is something unusual
    String in = "$(123.00)".replaceAll("\\(", "").replace(')', ' ').trim();
    amount = validator.validate(in, Locale.US);
    assertNotNull(amount);

    amount = validator.validate("invalid", Locale.US);
    assertNull(amount);
}

Altri suggerimenti

You could try DecimalFormat. It allows you to handle positive and negative value patterns separately using ;:

List<String> list = new ArrayList<>();
list.add("$123.00");
list.add("$(123.05)");
list.add("incorrectString");

NumberFormat nf = new DecimalFormat("¤#.00;¤(#.00)", new DecimalFormatSymbols(Locale.US));
try {
    for(String str : list){
        nf.parse(str);
    }
} catch (ParseException e) {
    System.out.println(e.getMessage());
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top