GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes problem with exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem with exception" Watch "problem with exception" New topic
Author

problem with exception

Robert Leon
Greenhorn

Joined: Sep 26, 2011
Posts: 11
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

Joined: Aug 18, 2009
Posts: 174

I tried to read what you're trying to achieve three times but no success, sorry.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Likewise. Could you please rephrase your question, but this time with actual punctuation and capital letters at the start of sentences?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Robert Leon
Greenhorn

Joined: Sep 26, 2011
Posts: 11
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

Joined: Oct 13, 2005
Posts: 39100
    
  23
Regular expressions?
Robert Leon
Greenhorn

Joined: Sep 26, 2011
Posts: 11
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Sep 26, 2011
Posts: 11
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

Joined: Sep 26, 2011
Posts: 11
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

Joined: Oct 13, 2005
Posts: 39100
    
  23
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

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

Joined: Oct 13, 2005
Posts: 39100
    
  23
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

Joined: Aug 18, 2009
Posts: 174

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

Joined: Oct 13, 2005
Posts: 39100
    
  23
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

Joined: Oct 13, 2005
Posts: 39100
    
  23
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.
 
GeeCON Prague 2014
 
subject: problem with exception