• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validate IP Address with Java Regular Expression example  RSS feed

 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi every one
I am trying to implement an example (Book* : Java SE 7 ..By S G Ganesh) for validating an IP address but it fails to validate a valid IP addresses. I found another example on the internet(**) and it works super fine, no problem at all. I edited the code (the one I got from internet) into the exact format like book and it still works super but i don't understand why the books' example doesn't work though both look exactly the same now ,further more, how can i compare String x and y for equality?
Can any one please help, I don't know what I am missing. Thanking you all in advance

OutPut:
false 35 35
253.205.188.123 is valid? true
253.205.188.123 is valid? false

*:Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 ... By S G Ganesh, Tushar Sharma.
// This program demonstrates how we can validate an IP address

**http://examples.javacodegeeks.com/core-java/util/regex/matcher/validate-ip-address-with-java-regular-expression-example/
// this is only part i copied from internet

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your "one" and "two" are simply not equal. I'm not sure how you did it, but the dash is not the same character in both.

I see the same thing with your x and y variables.
 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@fred rosenberger, Super man , wonderful.. yes you are very right, i never looked into that. I changed the dash sign with minus and now it works fantastic. Finally got it, it was a typo mistake in the book. Thanks once again for such an instant reply.
 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@fred rosenberger
I'm not sure how you did it, but the dash is not the same character in both.

Yes you are very right.
I still wonder how they did it in the book, because on my keyboard i have only two dash signs, I tried the code with both signs and it works perfectly. The dash in the book is a little bit longer then the dashes on my keyboard.
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this the other day ... I first restricted the TextField to only allow 0-9 and . Then I threw it at a RexEx pattern, and if it matched, I then tested each octet simultaneously in one custom procedure:

 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure if this will help anyone, but in order to restrict the TextField to only numbers and the period, I bound the On Key Typed property to this code:

 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Michael D Sims. Thanks very much for putting your time and effort to write this example. Off course it helps a lot Java newbies like me.
 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

@Michael D Sims
Hi again

in order to restrict the TextField to only numbers and the period, I bound the On Key Typed property to this code:


I am trying to implement the TextField restriction code, but i am not sure where to put it inside class, i get compilation error.
Any help would be much appreciated.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riaz Ud Din wrote:@fred rosenberger
I'm not sure how you did it, but the dash is not the same character in both.

Yes you are very right.
I still wonder how they did it in the book, because on my keyboard i have only two dash signs, I tried the code with both signs and it works perfectly. The dash in the book is a little bit longer then the dashes on my keyboard.


In the book must be used not mathematical "minus" symbol, but simply dash symbol, which is special character probably converted into it by one of the posh text editors such as "MS Word" or "Open Office". Keyboard contains only "minus" symbol.

To avoid such a things, copy text first to a raw text format (I presume that you use one of those OS, so you can use the best known editors NOTEPAD++ for windows, TextWrangler for Mac's)
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riaz Ud Din wrote:
@Michael D Sims
I am trying to implement the TextField restriction code, but i am not sure where to put it inside class, i get compilation error.
Any help would be much appreciated.

OK, in my case, I am using JavaFX ... I'm not sure what you are using to build your GUI, but in my case, I have this method in my controller class:

As you can see, I actually use the code to monitor two different TextFields, one is when the user types in the IP address and the other is when they type in the port number (relevant for a database connection in my case). The port number should never contain a dot, so I had to specify two different checks (via the charCheck String).

Now, when I open my JavaFX form in SceneBuilder, then select the TextField I need to apply the filter on, on the right side of SceneBuilder, I have a property under the Code: section called On Key Typed, and if my method (the code I pasted above) is already in my controller class, then in the drop down box, I will have an option to chose that code to apply to that property of the control.

Alternatively, when I look at my JavaFX fxml file (Which is what SceneBuilder modifies directly) and look at the code that defines those TextFields, they look like this:

Notice the onKeyTyped="#checkKeyPressed" portion of the TextFields definition. That is literally what binds the code to the Field.

If you are not using JavaFX, at least you WILL be using something that will use a similar means of binding code to events on controls. Although I do not know if every way of building a GUI in Java implements control methods as simply as JavaFX does, as JavaFX is the only way I've ever done it. Actually, this little project I'm working on is the first time I've programmed in almost 8 years and the first time I've messed with Java since college which was about 10 years ago, so you and I are both newbies in that sense.

 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Liutauras Vilda
To avoid such a things, copy text first to a raw text format (I presume that you use one of those OS, so you can use the best known editors NOTEPAD++ for windows, TextWrangler for Mac's)


Thanks for very such helpful info..
 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Michael D Sims
If you are not using JavaFX, at least you WILL be using something that will use a similar means of binding code to events on controls. Although I do not know if every way of building a GUI in Java implements control methods as simply as JavaFX does, as JavaFX is the only way I've ever done it. Actually, this little project I'm working on is the first time I've programmed in almost 8 years and the first time I've messed with Java since college which was about 10 years ago, so you and I are both newbies in that sense.


hmmmmmm, Okay, i get it now, ya it must have to be implemented with GUI. I am using Eclipse GUI only for android projects. I am just getting ready for Java OCP exam because android projects are mostly done in Java, so i decided why not to take java exams as well, killing two birds with one stone... . Me too, messing with Java (because of android projects) since Uni which was like yours about 10 years ago and its really a comfort to know that i am not alone in this java boat turbulence .
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Riaz Ud Din wrote:I am using Eclipse GUI only for android projects.

Well .... it *IS* supposed to be write once, run anywhere ... right? hehe
Riaz Ud Din wrote:I am just getting ready for Java OCP exam because android projects are mostly done in Java, so i decided why not to take java exams as well, killing two birds with one stone...

But .... then you'd have two dead birds ...
Riaz Ud Din wrote:its really a comfort to know that i am not alone in this java boat turbulence .

Oh, I've been on this boat for a few months now ... best advice, when you feel the boat a-swayin' ... grab the nearest metal object that is fastened to the boat and HANG ON! ... I've seen one or two get washed away already ... sad really...
 
Michael D Sims
Ranch Hand
Posts: 113
1
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, I chose JavaFX because it appeared to be the easiest way to build GUI apps, and so far, so good. I needed something with a small learning curve since it has been so long since I've programmed. A little googling turned this up, you may be interested in it:

JavaFX development for Android

And it should not matter one bit, what you are using for your IDE ... JavaFX and your IDE are not in any way related ... JavaFX is just another way of making GUIs in Java (Swing being another way among others) ... and I believe JavaFX is actually the current Oracle approved way of doing it ... as in they are throwing all their eggs into that basket and slowly abandoning the other ways of doing it - but I may be wrong about that, I'm not sure.
 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Michael D Sims
Thank you so much for the tips. I will surely use JavaFX. finger cross
 
Riaz Ud Din
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But .... then you'd have two dead birds ...

LOL...
ouchhhhhhhhhhhhhhhhhh


best advice, when you feel the boat a-swayin' ... grab the nearest metal object that is fastened to the boat and HANG ON

LOL..
Thanks for the advice..i will definitely follow your advice....
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!