• 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

Regular Expressions in an if block

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I am fairly new to Java and need help with this code I am trying to write.

Fairly simple really, CheckPassFail, enter a number and greater than 50 return pass else fail. Only, I want to eliminate everything but numeric characters with the use of a Regex, i.e., (\\d+?)

I have written this,




Output: Enter the value of a
5
Invalid!

Can anybody, please tell me where I am going wrong?

Thank you very much in anticipation,

Nikhil

P.S. I know how to write this without regex!
 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch! Please UseCodeTags (<- read) when posting your code. I have added them for you this time.

As per your question, the first thing you did wrong is the line 19 of your code - you are trying to compare two object references, both of which are of String type. Since these are not the same, you are always getting false as a result, hence the output you get.

However, the approach is still wrong, even if the above wouldn't be a problem. I would recommend you read Regular Expressions tutorial to get an idea on how to use them. As a hint, for your problem you would probably need to check String#replaceAll() method.
 
Nikhil Shanbhag
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Kemal,

So basically, I need to get a more solid grip on RegEx before I try this stunt!

What I did was tried to use an Eclipse plugin, named QuickREx, where I got the said RegEx and tried to plug that in my code!

But I will try that tutorial first and if still unclear, will head back to moose!

Thank you, also, for the code tags and I can now see why you use it!

Cheers,
Nikhil
 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why you are not using hasNextInt() and nextInt() method of Scanner class for validation and assignment ?
 
Nikhil Shanbhag
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Khuzema,

Thank you for looking up my post.

Initially, I declared,



But then when I tried to use,



It would throw an error with String (Apologies, I dont quite remember the error now!)

Hence, I resorted to this and then parsed it to make a match!

Best,
Nikhil
 
Khuzema Dharwala
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the modified version of your program.

 
Nikhil Shanbhag
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Khuzema,

Thank you for the code.

I admit, I did not think of checking the IF statement to look for an int.

However, that being said, I can manage to write a program without the RegEx. The purpose was to use RegEx in a conditional statement which flummoxed me.

Thank you nonetheless.

I am reading up on the RegEx in Java and hopefully, I will decipher what I did wrong!

Best,
Nikhil
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What exactly are you trying to do with this:

if (a != "\\d+?" ) {

Are you trying to see if the String 'a' literally contains "back-slash back-slash character 'd' character '+' character '?'"?

Or are you trying to test to see if the string 'a' contains one or more digits?

You are doing the former, but I think you want to do the latter.

Java doesn't let you do it this way.
 
Nikhil Shanbhag
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indeed Fred.

Thank you for pointing that out.

Its just that I was trying various permutations and combinations to get the RegEx as the condition and ended up with that, frustrated, I posted this note.

However, as already pointed out, I am going through those guidelines and tutorials now to get a gist of it.

Thank you indeed!

Best,
Nikhil
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nikhil Shanbhag wrote: . . . Its just that I was trying various permutations and combinations . . .

Trying various things sounds to me like a policy for disaster; you can try 1000000 things and maybe one will work, or you can learn to do it correctly and your first attempt will work.
I would suggest hasNextInt, as previously mentioned, may be the best solution. In my opinion, it would be better still if you wrote a utility class to use that Scanner in.
If you insist on using regexes, try a tutorial, so you know how to use them, and it will work nicely.
reply
    Bookmark Topic Watch Topic
  • New Topic