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



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

Answer / 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

More Core Java Interview Questions

Can we have any code between try and catch blocks?

0 Answers  


Define Compiling?

3 Answers  


How many types of threads are there in java?

0 Answers  


What is immutable class? how to make a Class explicitly "Immutable"?Wap to make a class explicitly immutable.

3 Answers  


How to validate the request (Eg:user name and password) in session(http session)? not in LDAP server ?

1 Answers   Saksoft,






How do you differentiate abstract class from interface?

0 Answers  


How to reverse string in java?

0 Answers  


What is the difference between a field variable and a local variable?

0 Answers  


What about abstract classes in java?

0 Answers  


Explain the difference between a Thread and a Process.

0 Answers   Ciena,


Does java list allow null?

0 Answers  


Can we override data members in java?

0 Answers  


Categories