please helpme to write a funtion in java.


write a functionto validate ip address from a given string. using string tokenizer. ex:- 192.160.1.5


please send me the code.


sanjay

void function() {
    while (true) {
       System.out.println("I was too lazy to do my own homework so I got this piece of code off the web!");
    }
}

To help get you started:

import java.util.*;
 
public class Playing {
    
    public static void main(String[] args) {
        String ip = new String("127.0.0.1");
        StringTokenizer st = new StringTokenizer(ip, ".");
        while (st.hasMoreTokens())
        {
            // your code here
            System.out.println("token: " + st.nextToken());            
        }
        
    }
}
Member Avatar for iamthwee

From Java's StringTokenizer API-docs:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

i

I assume someone needs to tell the teacher that. LOL
I was wondering why one wouldn't just regex for validation.

probably still using a 1.1 based book...
Or the kid misunderstood the assignment and it said to "tokenise a string", the first thing he found when looking for that was "StringTokenizer" (less likely, as it assumes some initiative on the part of the OP).

Define validate!!!!
Define validate!!!!
Define validate!!!!


Assuming you mean verify it could be a valid IP address even if no one is using it, then you can do this with String Tokenizer or Stream Tokenizer

String tokenizer splits the string into chunks using a delimiter.
You will have to consider IP4 and IP6 formats and at the very least verify the number of chunks is correct and the values of each chunk are in range.

If you mean is there something there when you try to connect that is a whole new ball game and I would suggest thinking carefully why you would want to do that from code.

It is also possible ( and I would look it up if I needed to do so) that sun have built the check you want into their API. In that case use SUN's methods. Chances are they will have done a better job thab you could.

please helpme to write a funtion in java.


write a functionto validate ip address from a given string. using string tokenizer. ex:- 192.160.1.5


please send me the code.


sanjay

Define validate!!!!
Define validate!!!!
Define validate!!!!


Assuming you mean verify it could be a valid IP address even if no one is using it, then you can do this with String Tokenizer or Stream Tokenizer

String tokenizer splits the string into chunks using a delimiter.
You will have to consider IP4 and IP6 formats and at the very least verify the number of chunks is correct and the values of each chunk are in range.

If you mean is there something there when you try to connect that is a whole new ball game and I would suggest thinking carefully why you would want to do that from code.

It is also possible ( and I would look it up if I needed to do so) that sun have built the check you want into their API. In that case use SUN's methods. Chances are they will have done a better job thab you could.

Um, I'm sorry, but what part of "using string tokenizer" did you not understand. I am quite sure the teacher would be satisifed with something that checks that it is four numbers between 0 and 255 seperated by a dot. I am fairly sure that this is a class exercise in the Strings and converting them to ints and not any actual networking exercise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.