Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

validating a format xxx.xxxx

Posted on 2008-06-12
Medium Priority
317 Views
hi guys

I have a requirment where i have a decimal field 'rate'  and rate
can be  in format xxx or xxx.xxxx

for example  rate can be 123 or 123.0000  or 123.3333. These are all valid rates

any idea how i can validate the rate in my java class

String rate = null;
if(rate is not in format xxx or xxx.xxxx)
{

//throw error

}

any idea, how i do this?

thanks
J
0
Question by:jaggernat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4
• 2
• +1

LVL 23

Expert Comment

ID: 21770113
You can use this,

Number x = new DecimalFormat("###.###").parse("122.222");
0

LVL 10

Author Comment

ID: 21770205
ok,thanks but i dint completely understand

>>>Number x = new DecimalFormat("###.###").parse("122.222");

this is what i have so far

String rate = "";
if (rate != "" && rate.length() > 0)

{
how do i use the above code here (Number x = new DecimalFormat("###.###").parse("122.222");)
}

thanks
0

LVL 6

Expert Comment

ID: 21770270

``````String rate = "122.222";
try {
Number x = new DecimalFormat("###.###").parse(rate);
} catch (ParseException ex) {
ex.printStackTrace();
}
``````
0

LVL 23

Accepted Solution

Ajay-Singh earned 800 total points
ID: 21770305
The idea is parse the string using DecimalFormat,

String rate = ...
if(rate != null && rate.length() > 0) {
Number x = new DecimalFormat("###.###").parse(rate);
if(x==null) {
// Parse error, throw the exception.
}
}
0

LVL 6

Assisted Solution

manuel_m earned 400 total points
ID: 21770595
ok, but an exception should be catched anyway. rate could be not parseable and then you have an exception .

Taking Ajay-Singhs answer and expand it with try/catch:

String rate = ...
if(rate != null && rate.length() > 0) {
try {
Number x = new DecimalFormat("###.###").parse(rate);
if(x==null) {
// Parse error, throw the exception.
}
} catch (ParseException ex) {
// Parse error, throw the exception.
}
}
0

LVL 86

Assisted Solution

CEHJ earned 400 total points
ID: 21770732
>>
I have a requirment where i have a decimal field 'rate'  and rate
can be  in format xxx or xxx.xxxx
>>

Try the below:
``````boolean valid = rate.matches("\\d{3}(?:\\.\\d{4})?");
``````
0

LVL 86

Expert Comment

ID: 21770746
(My comment takes your comment literally)
0

LVL 10

Author Comment

ID: 21771165
thanks all
0

LVL 86

Expert Comment

ID: 21771258
You might as well use Double.parseDouble, since those NumberFormat let most things through. Try

3.splat

for instance ;-)
0

LVL 6

Expert Comment

ID: 21772727
hello CEHJ

could you please explain this part of the regex?
(?: .... )?

thank you.

manuel
0

LVL 86

Expert Comment

ID: 21772873
a. Don't save it
b. Allow one or zero occurences of it

(I assume you know what 'it' is - if not, let me know)
0

LVL 6

Expert Comment

ID: 21772980
Yes, I know what 'it' is. Thank you for your response and your explanation!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to aâ€¦
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used byâ€¦
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Downâ€¦
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompaâ€¦
Suggested Courses
Course of the Month6 days, 18 hours left to enroll