2

I want to make a perfect integer dialog on Java, where you cannot type letters, only numbers

Here is my code:

public class PMain {
    public static void main(String args[]) {
        String a = JOptionPane.showInputDialog("Type the number for 'A'");
        int A = Integer.parseInt(a.trim());
        String b = JOptionPane.showInputDialog("Type the number for 'B'");
        int B = Integer.parseInt(b.trim());
    }
}
Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
  • 1
    possible duplicate of [JOptionPane Input to int](http://stackoverflow.com/questions/3120922/joptionpane-input-to-int) – Uli Köhler Jan 12 '14 at 17:50
  • Doesn't sound too perfect to me. I'd expect the *perfect* dialog to support scientific notation like `1e6` for a million. :) – mike3996 Jan 12 '14 at 17:53

1 Answers1

3

You could make your own Dialog for that (not JOptionPane) and use JFormattedTextField and MaskFormatter. You can find an Example here.

Carsten Hoffmann
  • 901
  • 5
  • 14