Here's the deal.This is my school assignment.To make calculator using JRadioButtons and JButton and JOptionPane - showMessageDialog (in short).
I did it all, and i miss only one thing, that is:
When i enter numbers in two JTextFields, in case that this entered number is decimal(float, double) i should get showMessageDialog that says "You can only enter whole numbers"
That's the whole deal.How do i do that?
By the way, yes i am new to Java
Ross Einmart wrote:Nothing happens when i input a wrong number, it just won't calculate with decimals (as I intended).
And as I said that's fine I just need showMessageDialog.
Trust me, something happens. Not in the user interface, but check the console. You can use that "event" to catch (hint hint) users entering incorrect numbers.
Amolk Patil wrote:
Nice try, but there are some problems here.
1) strValidate.equals(null) always returns false, so either the body is executed or a NullPointerException is thrown. Use == and != to compare to null.
2) don't use toCharArray() to iterate over the contents. Use length() and charAt(int).
3) don't use an empty body for the if statement, just to be able to execute the else statement. Negate the guard instead.
4) what will you do if I enter 9876543210? That's valid according to your method, but it's too large for ints.
Joined: Feb 08, 2011
Thanks Rob Spoor for your valuable suggestions in my code.
I think we have to handle NumberFormatException exception here.
True, but only since Java 5.0. In Java 1.4 and before, calling methods like setLayout, add and remove directly on frames / dialogs would throw an Error. Not an exception, no, a real genuine Error:
A lot of people still are using getContentPane() because of that.