Solved

validating a format xxx.xxxx

Posted on 2008-06-12
12
281 Views
Last Modified: 2010-03-30
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
Comment
Question by:jaggernat
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
Comment Utility
You can use this,
 
Number x = new DecimalFormat("###.###").parse("122.222");
0
 
LVL 10

Author Comment

by:jaggernat
Comment Utility
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

by:manuel_m
Comment Utility

String rate = "122.222";

try {

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

} catch (ParseException ex) {

  ex.printStackTrace();

}

Open in new window

0
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 200 total points
Comment Utility
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

by:manuel_m
manuel_m earned 100 total points
Comment Utility
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

by:CEHJ
CEHJ earned 100 total points
Comment Utility
>>
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})?");

Open in new window

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
(My comment takes your comment literally)
0
 
LVL 10

Author Comment

by:jaggernat
Comment Utility
thanks all
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You might as well use Double.parseDouble, since those NumberFormat let most things through. Try

3.splat

for instance ;-)
0
 
LVL 6

Expert Comment

by:manuel_m
Comment Utility
hello CEHJ

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

thank you.

manuel
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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

by:manuel_m
Comment Utility
Yes, I know what 'it' is. Thank you for your response and your explanation!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now