It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Regular Expressions in an if block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regular Expressions in an if block" Watch "Regular Expressions in an if block" New topic
Author

Regular Expressions in an if block

Nikhil Shanbhag
Greenhorn

Joined: May 21, 2013
Posts: 7
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!
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
Nikhil Shanbhag
Greenhorn

Joined: May 21, 2013
Posts: 7
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
Khuzema Dharwala
Ranch Hand

Joined: Apr 26, 2010
Posts: 46

Why you are not using hasNextInt() and nextInt() method of Scanner class for validation and assignment ?


OCJP 6
Nikhil Shanbhag
Greenhorn

Joined: May 21, 2013
Posts: 7
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

Joined: Apr 26, 2010
Posts: 46

Here is the modified version of your program.

Nikhil Shanbhag
Greenhorn

Joined: May 21, 2013
Posts: 7
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11154
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Nikhil Shanbhag
Greenhorn

Joined: May 21, 2013
Posts: 7
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37962
    
  22
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expressions in an if block
 
Similar Threads
weird hasNextDouble behavior.
inheritance with static print method
Running...tally...?
Largest/Greatest of n numbers
Problem in looping