Write a program to read a string and validate the IP address. Print “Valid” if the IP address is valid, else print “Invalid”.
Input and Output Format
- Input consists of a string that corresponds to an IP.
- The output consists of a string(“Valid” or “Invalid”).
Refer sample output for formatting specifications
Note: An IP address(IPv4) has the format a.b.c.d where a,b,c,d are numbers between 0-255.
Sample Input 1:
132.145.184.210
Sample Output 1:
Valid
Sample Input 2:
132.145.184.290
Sample Output 2:
Invalid
Program to Check IP Address in Java
Following are the steps to check for validation of IP:
- Input a string from the user.
- Pass the string to validateAddress() method.
- Inside the method, declare a variable of boolean type and make it false.
- Now, use the string tokenizer class to break the string into tokens by using ‘.’ as a delimiter. Extract each token and store it in an integer variable a,b,c, and d.
- Now, check for the range of each value in variables a,b,c, and d. It should be between 0-255. If this is true then, make the boolean value true.
- At last, return this value.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ipAddress = sc.nextLine();
boolean b = validateIpAddress(ipAddress);
if (b == true)
System.out.println("Valid");
else
System.out.println("Invalid");
}
public static boolean validateIpAddress(String ipAddress) {
boolean b1 = false;
StringTokenizer t = new StringTokenizer(ipAddress, ".");
int a = Integer.parseInt(t.nextToken());
int b = Integer.parseInt(t.nextToken());
int c = Integer.parseInt(t.nextToken());
int d = Integer.parseInt(t.nextToken());
if ((a >= 0 && a <= 255) && (b >= 0 && b <= 255)
&& (c >= 0 && c <= 255) && (d >= 0 && d <= 255))
b1 = true;
return b1;
}
}
Output

Validate IP using regex in Java
Write a code to validate an IP Address using regular expression. Let, 112.12.55.215 be an IP address, if it is valid then the program should return ‘Valid’.
Regular Expression:
range = (\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5]) //range from 0 to 255
IPv4 = range + "\\." + range + "\\." + range + "\\." + range; // full IP address
- \d – It means any digits in regex. \\d{1,2} has one or two-digit number.
- (0|1)\\d{2} – It means three digit number starting with 0 | 1.
- 2[0-4] \\d – It means a number between 200 – 249.
- 25[0-5] – It means a number between 250-255.
Validate IP Program using regex in Java
Following are the steps to validate IPv4 using regex:
- Input string from the user.
- Use the regular expression to validate the IP address.
- If the input string matches with the regex given above then print “Valid” else print “Invalid”.
package com.demo3;
import java.util.Scanner;
import java.util.StringTokenizer;
public class IPValidator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ipAddress = sc.nextLine();
String range = "(\\d{1,2}|(0|1)\\d{2}|2[0-4]\\d|25[0-5])";
if (ipAddress.matches(range + "\\." + range + "\\." + range + "\\." + range)) {
System.out.println("Valid");
}
else{
System.out.println("Invalid");
}
}
}
Output

Validate IP Address using Library
Here, we will use the external library to validate IP Addresses. We have an Apache-common validator to validate both IPv4 & IPv6 addresses. Use the following steps to use it:
- You need to create a Maven project using any IDE (We are using Eclipse IDE)
- Next, add the following dependency into the pom.xml file
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>1.7</version>
</dependency>
- At last, inside an App.java file use the methods provided by Apache-common library and check if the address is valid or not. InetAddressValidator is the class that provides the following methods.
- isValid(String inetAddress)– It returns true if the address is either IPV4 or IPV6.
- isValidInet4Address(String arg) – It return true if the address is IPV4.
- isValidInet6Address(String arg) – It return true if the address is IPV6.
Program to validate IP Address using Apache Common Validator
package com.IPAddress;
import java.util.Scanner;
import org.apache.commons.validator.routines.InetAddressValidator;
/**
* Hello world!
*
*/
public class App {
public static void main(String[] args) {
InetAddressValidator validator = new InetAddressValidator();
Scanner sc = new Scanner(System.in);
String ipAddress = sc.nextLine();
if (validator.isValid(ipAddress)) {
System.out.println("It is a Valid Address");
}
if (validator.isValidInet4Address(ipAddress)) {
System.out.println("It is a valid IPv4 Address");
}
if (validator.isValidInet6Address(ipAddress)) {
System.out.println("It is a valid IPv6 Address");
}
}
}
Output


Thus, in this way, we learn how to validate IP addresses in Java.