1

I have a error when I tried do:

private Double isEmpty(String some) {

    LOGGER.info("Se ha llamado a isEmpty, valor: " + some);
    Double empty = null;

    try {
        if (some != null) {
            // Reemplazar porque nosotros usamos los . para los miles
            if(some.contains(".")) {
                some = some.replaceAll(".", "");
            }
            if(some.contains(",")) {
                some = some.replaceAll(",", ".");
            }
            empty = Double.parseDouble(some);
            LOGGER.info("Nuevo valor en isEmpty en Double: " + some);
        }
    } catch (Exception ex) {
        LOGGER.error("*******************");
        LOGGER.error("************ Falló la ejecución isEmpty *************");
        LOGGER.error(ex.getMessage() + ex);
        LOGGER.error(ex.getLocalizedMessage());
        LOGGER.error("*******************");
    }

    return empty;
}

In my country "1.000" this is one thousand, In America is One... Then I replace all the characters for format in English...

For example in my country : "2.035,75€" --> Two thousands thirty five with seventy five ... I can't parse this. I do replace ->

"2035.75" for that Java admit this number. BUT I get error ->

 Se ha llamado a isEmpty, valor: 19.425
2018-10-16 ERROR 6197 --- [nio-8090-exec-5] Imp  : *******************
2018-10-16 ERROR 6197 --- [nio-8090-exec-5] Imp  : ************ Falló la ejecución isEmpty *************
2018-10-16 ERROR 6197 --- [nio-8090-exec-5] Imp  : empty Stringjava.lang.NumberFormatException: empty String
2018-10-16  ERROR 6197 --- [nio-8090-exec-5] Imp  : empty String
2018-10-16  ERROR 6197 --- [nio-8090-exec-5] Imp  : *******************
luk2302
  • 55,258
  • 23
  • 97
  • 137
EduBw
  • 866
  • 5
  • 20
  • 40

5 Answers5

3

replaceAll takes a regex and . is the regex char that matches anything, so some.replaceAll(".", "") replaces everything with nothing. You would need to esapce .:

some = some.replaceAll("\\.", "");

or simply use replace insteadf, which does not take in a regex but a regular CharSequence or char:

some = some.replace(".", "");

Note that if you use an IDE you might get a highlight on the regex part, I would recommnd using one:

enter image description here

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • 1
    ... or just use [`replace`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#replace-java.lang.CharSequence-java.lang.CharSequence-) – khelwood Oct 16 '18 at 08:42
  • @khelwood but there may be more than one match to be replaced. – luk2302 Oct 16 '18 at 08:43
  • 3
    Yes, that's what replace does. "Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence." It just doesn't use a regular expression, so you don't have to escape anything. – khelwood Oct 16 '18 at 08:44
  • @khelwood you are correct, I just programmed in js and got confused, there the replace with a normal string only replaces once... – luk2302 Oct 16 '18 at 08:48
  • @khelwood if you have more than one decimal separator in your string, you should fail anyway - what is the expected value of "123.45.67"? – spi Oct 16 '18 at 08:48
  • @spi In the OP's question, `.` is a thousands separator. – khelwood Oct 16 '18 at 08:48
  • hi All, but the problem doesn't "." is when I use Double.parseDouble "19.425" ... – EduBw Oct 16 '18 at 08:52
  • @EduBw please read my first sentence very carefully, I write "replaces everything with nothing" for a reason, what does "19.425" after everything is replaced with nothing? – luk2302 Oct 16 '18 at 08:54
3

You should consider using localization for your problem. Please see Converting different countrys currency to double using java

You could try the following:

public BigDecimal parseMoney(String some) {
    try {
        Locale yourLocale = new Locale("es", "ES");
        return parse(some, yourLocale);
    } catch (ParseException pe) {
        return null;
    }
}

public BigDecimal parse(String amount, Locale locale) throws ParseException {
    final NumberFormat format = NumberFormat.getNumberInstance(locale);
    if (format instanceof DecimalFormat) {
        ((DecimalFormat) format).setParseBigDecimal(true);
    }
    return (BigDecimal) format.parse(amount);
}

Please note that

  • your method isEmpty does not do what its name suggests so I changed it to parseMoney
  • when dealing with money you should not rely on Double so I changed the return type to BigDecimal
  • I assumed you are located in a Spanish speaking country so I chose the Locale("es", "ES") but make sure to adjust it as needed
Antimon
  • 241
  • 1
  • 10
  • 3
    Apart from the programming error this is the correct answer. But please include more content in this answer, right now it is more like a comment since basically link-only. How would OP have to change his code to do what you suggest? – luk2302 Oct 16 '18 at 08:50
  • Thanks for the hint. I added some more detail. – Antimon Oct 16 '18 at 10:07
1

Change this line

some = some.replaceAll(".", "");

to

some = s.replaceAll("\\.", "");

Because . (dot) means matching with all.

drowny
  • 2,067
  • 11
  • 19
1

replaceAll needs two parameter: 1st is regex and 2nd is substring which is to be replaced.

so in regex . has meaning of any single character, so your each and every character is replaced with null value as you code: some = some.replaceAll(".", "")

try this: some = some.replaceAll("\\.", "")

Nagesh Mhapadi
  • 152
  • 3
  • 14
-1

I think you don't need to do replaceAll. Below code should work

        String some = "19.27";
        double d = Double.parseDouble(some);
        System.out.println("String " + some + " is parse to double value : " + d);
Deepak Jain
  • 347
  • 1
  • 6