File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField with JOptionPane - showMessageDialog (please help)

 
Ross Einmart
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks
By the way, yes i am new to Java

 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Ross!

Could you please UseCodeTags next time? I've added them for you this time.

What happens currently when you input a wrong number? Does something show up in the console? (hint hint)
 
Ross Einmart
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for UseCodeTags, i'll remember.
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.
 
Amolk Patil
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI

I thing you have to validate input string. Refer the modified code below.

Hope this will help you.

Good Luck


 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Amolk Patil
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob Spoor for your valuable suggestions in my code.

I think we have to handle NumberFormatException exception here.

Something like this..

 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catching that exception was what I was hinting at, yes. But you don't need your own validation anymore. The parsing to int and catching the exception will do that for you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't usually need to say getContentPane().add(). You can simply write add().
 
Rob Spoor
Sheriff
Pie
Posts: 20380
45
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ross Einmart
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys.
This really solved my problem.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic