matches not working
I am using the matches method to see if a value is in the format 9999 or 99.99, it works if no decimal places are put in but if i enter a value like 13.23 it goes to the notvalid.jsp page, can anybody see what i have done wrong?
boolean error =false;// set to true if error occures
if (MaxPriceForm.matches("^\\d+\\.\\d{2}$"))// this bit not working
{
error =false;// if price in the format 99.99
}
if (MaxPriceForm.matches("^\\d+$"))
{
error =false;// if price in format 9999999999 this bit works
}
else
{
error =true;
}
if (error ==true)
{
request.setAttribute("notvalid","whatShouldIputHere");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/notvalid.jsp");
dispatcher.forward(request, response);
}
else
{
// fill in later
}
I see nothing wrong with your regular expressions. I wonder about the API you're using for matching, though, and about the strings you are applying to the match. Can you provide a little bit more of the code, showing how all of the objects being used are initialized?- Adam
"13.23".matches("^\\d+\\.\\d{2}$")returns true.If you're getting false, then the string is not of that format, or you're not invoking that matches method.
jverda at 2007-7-11 22:31:02 >

thanks for the reply this is the full class file i am using:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class checkdata extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// Get the infor from html form
String categoryForm;
String MaxPriceForm;
categoryForm = request.getParameter("category");
MaxPriceForm = request.getParameter("MaxPrice");
boolean error = false; // set to true if error occures
if (MaxPriceForm.matches("^\\d+\\.\\d{2}$")) // this bit not working
{
error = false;// if price in the format 99.99
}
if (MaxPriceForm.matches("^\\d+$"))
{
error = false;// if price in format 9999999999
}
else
{
error = true;
}
if (error == true)
{
request.setAttribute("notvalid", "whatShouldIputHere");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/notvalid.jsp");
dispatcher.forward(request, response);
}
else
{
// fill in later
}
}
}
I tried using the method to check for values like 12.56 in a simple class file which printed true or false and it wors fine so i am sure i am using the correct code i just dont see why its not working with this class.
> I tried using the method to check for values like
> 12.56 in a simple class file which printed true or
> false and it wors fine so i am sure i am using the
> correct code i just dont see why its not working with
> this class.
Well, you know the regex works, so obivously either:
* The string you're sending is not what you think.
* The code you're executing is not what you think.
You'll need to do some debugging to see what's really happening.
jverda at 2007-7-11 22:31:02 >

it works fine for whole number but as soon as i enter a value like 12.22 or anything to 2 decimal places it detects it as a error which is not what i want, the data is entered via a html form so this is the only class file i have.
> Well, you know the regex works, so obivously either:
>
> * The string you're sending is not what you think.
>
> * The code you're executing is not what you think.
My money is that the string the OP is using isn't what's expected. In particular, I would check to see if it's possibly getting padded with extra white space. The ^ and $ in your regular expression could sting you. (Although, if I'm not mistaken, they're implied in the String.matches method anyway, aren't they?)
> > Well, you know the regex works, so obivously
> either:
> >
> > * The string you're sending is not what you think.
> >
> > * The code you're executing is not what you think.
>
> My money is that the string the OP is using isn't
> what's expected. In particular, I would check to see
> if it's possibly getting padded with extra white
> space.
Yeah, I was thinking something along those lines.
> The ^ and $ in your regular expression could
> sting you. (Although, if I'm not mistaken, they're
> implied in the String.matches method anyway, aren't
> they?)
Yes, matches requires the whole string to match, even w/o ^ and $.
jverda at 2007-7-11 22:31:02 >

Forget the regex. It has nothing to do with the error.
Look closely at this: if (x) {
err = false;
}
// Whether x was true or not will have no effect on what happens below.
if (y) {
err = false;
}
else {
err = true;
}
jverda at 2007-7-11 22:31:02 >

Side note:Prefer if (err) over if (err == true)The == true is just redundant clutter.
jverda at 2007-7-11 22:31:02 >

Another side note: you can replace this codeif (y) {
err = false;
} else {
err = true;
}
by this:err = !y;
I always prefer to have one line of code rather than five lines as long as the shorter code isn't unclear. I don't think it is unclear in this case.
Thank you for the reply i will try that but can you just explain the first one again have i written the if statement wrong? how should i have written it? Thank you again
Also the initialization at declaration is a bad idea with the structur the OP is using. It could leave you missing a case and the compiler being unable to tell you about it.
If OP sticks with that format, it should be:boolean error;
if (...) {
err =
}
... etc. ...
However, cleaning it all up, it could properly and cleanly be done with a single line:
boolean error = (something || something else);
No if/else needed.
Details left as an exercise for the OP.
jverda at 2007-7-11 22:31:02 >

> Forget the regex. It has nothing to do with the
> error.
>
> Look closely at this: > if (x) {
>err = false;
>
>
>
> // Whether x was true or not will have no effect on
> what happens below.
> if (y) {
>err = false;
>
> else {
>err = true;
>
>
Ah.. right. I missed that. What's missing is an "else if" instead of an "if". I didn't even look at that. I just looked at the regex's to see if there was anything wrong there.
> Thank you for the reply i will try that but can you
> just explain the first one again have i written the
> if statement wrong? how should i have written it?
> Thank you again
Study this carefully. Run it. Tihs is what your original code is doing.
public class IfTest {
public static void main(String[] args) throws Exception {
int x = 1;
boolean err = false;
if (x == 1) {
err = false;
System.out.println("x == 1, err = false");
}
if (x == 2) {
err = false;
System.out.println("x == 2, err = false");
}
else {
err = true;
System.out.println("x != 2, err = true");
}
}
}
But really, as mentioned earlier, you should skip the if else altogether and just do
bolean error = !(something || something else);
Make sure you understand why your current code isn't working though.
jverda at 2007-7-11 22:31:02 >

> Ah.. right. I missed that. What's missing is an> "else if" instead of an "if". I didn't even look at> that. I just looked at the regex's to see if there> was anything wrong there.Yeah, so did I at first.
jverda at 2007-7-21 19:45:31 >

Thanks everyone that works fine now.