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!");
}
}
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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());
}
}
}
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 13
Solved Threads: 4
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
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
I assume someone needs to tell the teacher that. LOL
I was wondering why one wouldn't just regex for validation.
nikkiH
Junior Poster in Training
79 posts since Dec 2006
Reputation Points: 13
Solved Threads: 4
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).
jwenting
duckman
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
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.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494