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

I have 100 records in a table with two rows. I need to display 10 records per page like Google Search. I need only the Logic(Pagination) in Pure Java. No JSP and all..Thanks in Advance...

2 Answers   Cybernet,


how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? Or extending thread class or implementing runnable interface. Which is better? : Java thread

0 Answers  


How many bytes is a string?

0 Answers  


What is static binding and where it occurs?

4 Answers  


Explain all java features with real time examples

0 Answers  






How to print nodes of a Binary tree?

0 Answers  


How do you write a scanner class in java?

0 Answers  


What does function identity () do?

0 Answers  


What is the role of the java.rmi.naming class?

0 Answers  


ublic class Java_Coding_Samples { public static void JavaHungr(NumberFormatException ae){ System.out.println("integer"); } public static void JavaHungry(Exception e){ System.out.println("string"); } public static void JavaHungry(ArithmeticException ae){ System.out.println("object"); } public static void main(String[] args) { JavaHungry(null); }

1 Answers  


What are the types of relation?

0 Answers  


what is custom tags with example?

3 Answers   Amdocs,


Categories