IP Address Validator using JAVA.

Subscribe to my newsletter and never miss my upcoming articles

Hello everyone!💜

IP address stands for internet protocol address. It is an identifying number that is associated with a specific computer or computer network. So, let's learn how to validate an IP address using Java.


CONTENTS


Understanding IP Address

An IP address is a numerical representation that uniquely identifies a specific interface on the network. IP address is of the form A.B.C.D where A,B,C,D represent numbers.

Example: 121.10.12.23, 124.34.22.1

  • IP address consists of several classes so that it can be used efficiently in various situations as per the requirement of hosts per network. Below given table consists of classes along with its ranges.

    ip_address_class.JPG

    Now, we know what is IP address and its format. So, let's implement this knowledge and build an IP address validator.

Code Time

Constraints

  1. IP address range is from 0 to 255.
  2. Leading zeroes are allowed.
  3. It should be of the form "A.B.C.D".

Aim

To give some IP addresses as input and validate whether the given IP address is valid or not.

Solution

import java.util.*;
public class Solution {

    public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     String IPAddress = sc.nextLine();
     String regex = "(\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5])"; 
    String pattern = regex + "\\."
              + regex + "\\."
              + regex + "\\."
              + regex; 
              if(IPAddress.matches(pattern)){
                  System.out.println("Valid");
              }
              else{
                  System.out.println("Invalid");
              }
    }
}

Explanation

To understand regex syntax check this cheatsheet.

1. In order to take an IP address as input we used Scanner class. We are going to store the input in the variable IPAddress.

import java.util.*;
public class Solution {

    public static void main(String[] args){
     Scanner sc = new Scanner(System.in);
     String IPAddress = sc.nextLine();

2. Now, to check whether an IP address is valid or not let's give a regex pattern.

  String regex = "(\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5])";

Let's break this into pieces and understand clearly.

  • A number having 1 or 2 digits- To satisfy this condition we wrote \\d{1,2}
  • | represents or
  • (0|1)\\d{2} says the number starts with 0 or 1 which counts as one digit and \\d{2} says it takes 2 digits. Therefore, we wrote this to say its a 3 digit number which starts with 0 or 1.
  • 2[0-4]\\d This represents numbers from 200 to 249. [0-4] takes any number ranging in 0 to 4. \\d means [0-9].
  • 25[0-5] this represents numbers from 250 to 255.

So, we have written a pattern to validate IP address.

3. We know, IP address is of form A.B.C.D. As we can see the pattern we wrote doesn't involve a dot(.). So, here we have taken our pattern like this.

 String pattern = regex + "\\."
              + regex + "\\."
              + regex + "\\."
              + regex;

regex is the variable name of the pattern we have written in step 2. For the variable named pattern, we have stored the pattern in IP address format.

4. Now, we have used matches method from pattern class in regex package to check if IP address which we gave as input matches the pattern.

 if(IPAddress.matches(pattern)){
                  System.out.println("Valid");
              }
              else{
                  System.out.println("Invalid");
              }

This method returns true if matches or else it returns false. I have used an if-else condition to print Valid if the condition is true and print Invalid if the condition is false.

Output

12.1.11.120
Valid
1234.12.1.0
Invalid

Hurray! We have successfully created an IP Address Validator using JAVA. Pat yourself.😋

Do let me know in the comments how this article helped you and do let me know if you want me to write on a particular topic.

Drop a like and do share your valuable feedback in comments!

I would love to connect with you through Twitter, LinkedIn, Github.

Articles you may also like

Comments (2)

DesignerAnna1's photo

Nice Article.<br> Also, I just realize that, if we remember the Regex Cheatsheet, then it will be super easy to create these kinds of small but useful projects.

🙌

Sai Laasya Vabilisetty's photo

Regex is easy and interesting. We just need little practice.