• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using NumberFormat to validate data entry

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem :
The user can key in an integer with comma formatting but the commas must be in the right place , leading minus is OK but no dec point allowed : 12,34 and 12 34 and 123.0 invalid. Such errors are flagged.

I can solve this by hand coding but I thought that NumberFormat would raise a ParseException instead the number is truncated to 12 12 and 123.
I know that Sun spec mentions truncation , read past JavaRanch questions and www.ibm.com/developerworks/java/library/j-numberformat/index.html
which "reveals passing warts that can be surprising".

What do Swing programmers do ?
Thank you.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you know about regular expressions? You can probably design one to match your input String, and throw an Exception, or clear the JTextField if it won't match.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know how to use regex.
But it's obvious that parse() for numbers and dates will pass almost anything as valid.
66/77/8888608 88:99 is valid DateFormat.SHORT.
Without wasting my time - it only seems to raise ParseException when a non-digit is used where a digit is required and invalid field delimiters (eg ? instead of /).
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The parse() methods are more of a "look at this string and do your best to convert it to the desired type" function than a validation function. You do really need to use a regex to validate the input, and if it is valid then use parse to convert.

And dates strings are really a bear to validate, especially if you want to handle localized date formats. Hence the use of calendars and date pickers - no validation necessary.
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Handles valid dates ?
What is going on here ? - trim() does not seem to fix the problem.
I must be doing something wrong.

import java.util.Date ;
import java.text.* ;
public class GBTest {
public static void main(String[] args) {
String hireDate = "25/12/39 00:00" ;
// String hireDate = "25/12/39" + " 00.00" ;
// Use concatenate and prints current date : 20080712 - weird
DateFormat df = DateFormat.getInstance() ;
Date dateObj = new Date() ;

try { dateObj = df.parse (hireDate) ; }
catch (ParseException pe ) { }

SimpleDateFormat maskSDF = new SimpleDateFormat ("yyyyMMdd") ;
String s = maskSDF.format(dateObj) ;
System.out.println (s) ; // 19391225 : OK Y2K yy > 50 then 20nn
}
}
 
Graeme Byers
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ignore that last posting - the HH:MM was invalid 00.00
Sorry.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
me too, I need more detailed info
Data Entry
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please explain your question. What sort of information?
 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic