Home arrow static arrow Java Programming [Archive] - NumberFormatException
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - NumberFormatException
This topic has 7 replies on 1 page.

Posts:46
Registered: 6/1/04
NumberFormatException  
Jul 30, 2004 9:27 AM



 
I am running a code that contains multiple text fields.
I use the try and catch methods to catch any NumbefFormatException that may happen due to the user inputs in the text fields.
I want to be able to use the selectAll() and requestFocus() function in order to highlight the particular text field that is in question.
How do you do that?
 

Posts:13,769
Registered: 00-11-29
Re: NumberFormatException  
Jul 30, 2004 9:39 AM (reply 1 of 7)



 
So your question really isn't about NumberFormatException at all.
 

Posts:46
Registered: 6/1/04
Re: NumberFormatException  
Jul 30, 2004 9:41 AM (reply 2 of 7)



 
Well, it's about whether you can format NumberFormatException in order to find out which textfield the exception came from.
 

Posts:316
Registered: 4/7/04
Re: NumberFormatException  
Jul 30, 2004 9:43 AM (reply 3 of 7)



 
I am running a code that contains multiple text
fields.
I use the try and catch methods to catch any
NumbefFormatException that may happen due to the user
inputs in the text fields.
I want to be able to use the selectAll() and
requestFocus() function in order to highlight the
particular text field that is in question.
How do you do that?

Minimize errors associated with user input in text fields by using JFormattedTextField.

See [url=http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/JTextField.html]JTextField[/url] API

 

Posts:13,769
Registered: 00-11-29
Re: NumberFormatException  
Jul 30, 2004 9:44 AM (reply 4 of 7)



 
You don't 'format Exceptions' unless you create them yourself.
 

Posts:316
Registered: 4/7/04
Re: NumberFormatException  
Jul 30, 2004 9:45 AM (reply 5 of 7)



 
Well, it's about whether you can format
NumberFormatException in order to find out which
textfield the exception came from.

Refer to the [url=http://java.sun.com/j2se/1.5.0/docs/api/index.html?javax/swing/JTextField.html]API[/url] for the answer.
 

Posts:329
Registered: 1/6/04
Re: NumberFormatException  
Jul 30, 2004 9:54 AM (reply 6 of 7)



 
I am running a code that contains multiple text
fields.
I use the try and catch methods to catch any
NumbefFormatException that may happen due to the user
inputs in the text fields.
I want to be able to use the selectAll() and
requestFocus() function in order to highlight the
particular text field that is in question.
How do you do that?

Sun has a Swing tutorial on validating input that might interest you:
http://java.sun.com/docs/books/tutorial/uiswing/misc/focus.html#inputVerification
 

Posts:316
Registered: 4/7/04
Re: NumberFormatException  
Jul 30, 2004 10:07 AM (reply 7 of 7)



 
The NumberFormatException is thrown at the point the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format. Therefore, if you just have a big try{}catch(NumberFormatException ex){} statement encompassing all the parseInt(), parseDouble(), ... code, you will not be able to find the field source. You'll need multiple try-catch blocks or better yet:
// not testedprivate int parseFieldInteger(JTextField field) throws NubmerFormatException {   try {      return Integer.parseInt(field.getText());   } catch(NumberFormatException ex) {      field.grabFocus();      throw new NumberFormatException ("Field not formatted: " + field.getText());   }}
 
This topic has 7 replies on 1 page.