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

x
?
Solved

validating a string before/after decimal point

Posted on 2008-10-07
16
Medium Priority
?
758 Views
Last Modified: 2008-10-07
hi guys

In my java class, I  have a field called
String amount = null ;


I have to validate the amount and make sure that amount folows the format xxxxx.xx
meaning it can have maximum 5 digits before the decimal and maximum of 2 digits after decimal
There fore the maximum a user can enter for amount is 99999.99

Any idea how i can do that.

Thanks very much
J
0
Comment
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
  • Learn & ask questions
  • 8
  • 8
16 Comments
 
LVL 14

Expert Comment

by:agriesser
ID: 22659887
You could use regular expressions for that (supported since JDK 1.4):

The test amount "12345.99" will match, anything that's not 5.2 will not match.

hth
String amount = "12345.99";
if(amount.matches("^[0-9]{5}\\.[0-9]{2}$"))
  System.out.println("Matches.");
else
  System.out.println("Nope.");

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22660292
ok, thanks. and also if i want to validate that the entered string is a number (zero to 99999.99) and not some garbage, , is there any reg ex to validate that too?

thanks v much
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660489
the above regex does exactly that.

asdfg.12  not valid
12345.1a not valid
123456. not valid
12300.  not valid
00001.00  valid
99999.99  valid
999.0  not valid
$1234.56  not valid
-1134.66 not valid

you get the idea :)
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 10

Author Comment

by:jaggernat
ID: 22660842
ok, thanks , got it. all of the above conditions are fine except

12300.  not valid  --> this should be valid coz before decimal it has 5 digits and after decimal it has none which is fine. The validation rule is  such that only if more than 5 digits are entered before the decimal and more than 2 digits are entered after the decimal , it should throw error.

so similarly 999.0  is valid coz  999 (less than 3 digits is valid)  and 0 (one digit after decimal is valid)

thanks

0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660953
Oh, ok, so the regex needs to be changed to:
untested, am currently not in front of my pc.
"^[0-9]{,5}\\.[0-9]{,2}$"

Open in new window

0
 
LVL 14

Expert Comment

by:agriesser
ID: 22660971
oh, another thing: is the decimal point mandatory? if it isn't, just tell me and I'll modify the regex again.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 22661203
thx, no the decimal point is not mandatory

thx
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22662255
OK, back at my computer, that's the solution:
String amount = "12345";
if(amount.matches("^[0-9]{0,5}\\.?[0-9]{0,2}$"))
  System.out.println("Matches.");
else
  System.out.println("Nope.");

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22662567
thx, i entered 999999
and  it did not go inside the IF condition
if(amount.matches("^[0-9]{0,5}\\.?[0-9]{0,2}$"))
{

}
not sure whats wrong
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22662593
999999 has 6 digits and you said it just needs to have 5 at max.
0
 
LVL 10

Author Comment

by:jaggernat
ID: 22662688
exactly, so it should go inside my if statement. ok, this is my exact IF  statement

String amount= formBean.getColumn(fld);  // amount value is 999999            
 if (!amount.trim().equals("")  &&   !amount.matches("^[0-9]{0,5}\\.?[0-9]{0,2}$"))
{
//show error
if user enters 999999 , it should come here coz its 6 digits (max entered can be 5)  
}

thx
0
 
LVL 14

Accepted Solution

by:
agriesser earned 2000 total points
ID: 22662776
Sorry, my fault (not my best day as it seems):
"^[0-9]{0,5}(\\.[0-9]{0,2})?$"

Open in new window

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22662962
it works, awesome. thanks, you did a great job actually.

0
 
LVL 10

Author Comment

by:jaggernat
ID: 22663009
any help with next question greatly appreciated

thanks
J
0
 
LVL 14

Expert Comment

by:agriesser
ID: 22663030
Sorry that it took me a few tries to provide the correct solution, I'm stuffed up with work currently... but good to hear that it finally works for you!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

721 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