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

what is the difference between static class and singleton class? can we create static class?

2 Answers   L&T, Octazen, Vamsi Labs,


what is difference between validation.xml & validation rules.xml?

8 Answers   BirlaSoft,


how many types of Inheritance?

0 Answers   Impetus,


What does replaceall do in java?

0 Answers  


Why we override equals() method?

0 Answers  






What is a protected void?

0 Answers  


What is runtime locatable code?

0 Answers   Cognizant,


What is difference between identifier and variable?

0 Answers  


Can we have multiple catch block for a try block?

0 Answers  


What is lambda in java?

0 Answers  


What is the difference between abstract classes and interfaces?

0 Answers  


What are methods of a class?

0 Answers  


Categories