2

I have a JFormattedTextField with the Name Hectare. The double type value is declared as shown below

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

Now the problem is that when i enter more than 3 digits, by default the comma is entered after 3 digits, e.g. 1,000. I have to add this value to some other value. But, due to this comma,I am unable to do it.

How can I remove comma and add this value to some other value?

mad
  • 3,493
  • 4
  • 23
  • 31
charan
  • 73
  • 2
  • 3
  • 10

4 Answers4

12

Call the getValue() instead of getText() on JFormattedTextField

naikus
  • 24,302
  • 4
  • 42
  • 43
8

A much easier solution

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

This will remove the grouping of the numbers using comma.

Unni Kris
  • 3,081
  • 4
  • 35
  • 57
1

You should 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;
}
jitm
  • 2,569
  • 10
  • 40
  • 55
0

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

i.e. your code should look like -

double hectare = Double.parseDouble(cultivationSize.replaceAll(",",""));
Gopi
  • 10,073
  • 4
  • 31
  • 45
  • You are relying on a specific default locale. There are different grouping-symbols for different locales (e.g. "." for Germany, " " for France). The other solutions are all independent of the locale. – Hardcoded Aug 29 '13 at 07:38