program to validate the IP address? Validity range should be
0 to 255

Answer Posted / jyoti

import java.util.StringTokenizer;
public class ValidateIP {

String IP = null;

public void validate(String IP)
{
this.IP=IP;
StringTokenizer st=new StringTokenizer
(IP,".");
int i=1;
while(st.hasMoreTokens())
{
int valid=checkRange(st.nextToken
());
if(valid == 0)
{
System.out.println("Token "
+ i + "is not valid");
}
else
{
System.out.println("Token "
+ i + "is valid");
}

i++;
}
}
public int checkRange(String tok)
{
int n=Integer.parseInt(tok);
if(n>=0 && n<=255)
{
return 1;
}
return 0;
}

public static void main(String args[])
{
ValidateIP v=new ValidateIP();
v.validate("192.165.256.1");
}

}

Is This Answer Correct ?    23 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is an example of a boolean?

567


What is early binding and late binding in java?

589


How many classes can any class inherit java?

504


What are parsers? Dom vs sax parser.

552


What is binary search in java?

553






Can we override private method in java?

611


Why arraylist is not synchronized in java example?

461


Can we compare two strings in java?

554


How do you check if a string is lexicographically in java?

494


What is the differences between c++ and java? Explain

580


Explain different types of thread priorities ?

616


Can we use this () and super () in a method?

525


What is use of super keyword?

565


What is Mutex (Mutual Exclusion Object) ?

619


How to write custom exception in java?

595