How to remove comma from TextField in Java

I have a JFormattedTextField named Hectare. A double type value is declared as shown below.

         String cultivationSize = JFormattedTextField3.getText();
         double hectare = Double.parseDouble(cultivationSize);

      

Now the problem is that when you enter more than three digits, by default the comma is entered after the three digits, for example. 1000. I have to add this value to some other value. But because of this comma, I cannot do it.

How do I remove the comma and add that value to another value?

+2


source to share


4 answers


use string.replace(",","");

i.e. your code should look like this:



double hectare = Double.parseDouble(cultivationSize.replaceAll(",",""));

      

-1


source


Call getValue () instead getText()

on the JFormattedTextField



+12


source


A simpler solution

Format format = NumberFormat.getIntegerInstance();
format.setGroupingUsed(false);
JFormattedTextField jtf = new JFormattedTextField(format);

      

This will remove the comma grouping of numbers.

+8


source


You must use MaskFormater like this:

zipField = new JFormattedTextField(
                    createFormatter("#####"));
...
protected MaskFormatter createFormatter(String s) {
    MaskFormatter formatter = null;
    try {
        formatter = new MaskFormatter(s);
    } catch (java.text.ParseException exc) {
        System.err.println("formatter is bad: " + exc.getMessage());
        System.exit(-1);
    }
    return formatter;
}

      

+1


source







All Articles