Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with exception

 
Robert Leon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi if someone can help me in creating an exception in an text field which only is valid when i press an strings from the keyboard as this is for a fullname textfield and show me error message if i press a number or especial character from the keyboard i will appreciate your help
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried to read what you're trying to achieve three times but no success, sorry.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Likewise. Could you please rephrase your question, but this time with actual punctuation and capital letters at the start of sentences?
 
Robert Leon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you guys for your time.
What i am doing is a simple tax calculator which have inputs textfield.
my first text field is to input a full Name and i need to validate it throwing an error message if I press from the keyboard a number or any especial character
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regular expressions?
 
Robert Leon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry that did not help me.
what i need is throwing an exception if i input an numeric value or an especial character in a text field
which is only validated if i input an string only
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a KeyListener to listen for typed keys or key pressed, or a DocumentListener attached to the text field's Document to listen for any changes to the content of the text field - not only when typing but also when pasting or calling setText.

As for showing the error, JOptionPane.showMessageDialog can be used for that.

Or perhaps a DocumentFilter is a better option, to simply disallow any wrong character to be filled in. If you use our search (especially the Swing / AWT forum) you should find some hints.
 
Robert Leon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you rob i already create the keyEvent i just need the method or a keyword for the exception i know how throw an exception when i input an string in a textfield i only need the other way around
throwing an exception if i input a number thank you
 
Robert Leon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i tried to do is this





but this validate a number and show me an error when is string please help
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

if () ... else throw new Exception(); ... catch() ...???

Never use Exceptions inside a method like that. All you achieve is making an if-else by a very inefficient route.
You have already been given suggestions how you might confirm the structure of your text.
What you are doing is parsing some text into an int, and comparing the text with that int (now boxed into an Integer) for equality. Since Strings and Integers are different classes, there is no way the equals() method will let that pass, so it instantly returns false. If it doesn't parse to an int, you suffer an exception.

What you can get from the KeyEvent object is the actual key which was pressed. And even the char which that key types. Now you can go to the Character class, which has methods telling you whether that char is a number, letter, space, etc etc.
Beware. I think it very likely there is somebody called Anne-Marie O’Neill living within 2 miles of here. I definitely know people called Anne-Marie and O’Neill. so you are going to have to accept the - and the ’ characters, too.

You might do better to put your code into the keyTyped() method. Also be sure to use the @Override annotation on all methods in XYZAdapter objects.
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, obviously. When an exception is thrown (you should really only catch NumberFormatException, and not throw an exception!), the number isn't a number, so you should not show a message. Also, text.equals(Integer.parseInt(text)) will always return false - text is a String, Integer.parseInt(text) is an int that is boxed into an Integer. A String and an Integer are never equal.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: . . . text is a String, Integer.parseInt(text) is an int that is boxed into an Integer. A String and an Integer are never equal.
Beat you by two minutes

(For once!)
 
Zandis Murāns
Ranch Hand
Posts: 174
Java Java ME Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Campbell Ritchie mentioned already, you should deal such things with regex.
Here's little example:



To understand my regex ^[^\d]*?$, please refer to this guide: http://www.regular-expressions.info/tutorial.html
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the sort of thing I meant, but you can't throw that Exception. None of the KeyListener methods declares an Exception, so you cannot throw an Exception. You will have to find some other way to handle the situation.
There are other solutions.
Why have we got a customer local variable which is never used?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you look in the KeyListener documentation, you find a link to ”How to write KeyListeners‟.

And that link doesn't work , so try this instead.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic