943,936 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5269
  • Java RSS
Feb 23rd, 2007
0

string tokenizer

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mengani123 is offline Offline
2 posts
since Feb 2007
Feb 23rd, 2007
0

Re: string tokenizer

Java Syntax (Toggle Plain Text)
  1. void function() {
  2. while (true) {
  3. System.out.println("I was too lazy to do my own homework so I got this piece of code off the web!");
  4. }
  5. }
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 23rd, 2007
0

Re: string tokenizer

To help get you started:

Java Syntax (Toggle Plain Text)
  1. import java.util.*;
  2.  
  3. public class Playing {
  4.  
  5. public static void main(String[] args) {
  6. String ip = new String("127.0.0.1");
  7. StringTokenizer st = new StringTokenizer(ip, ".");
  8. while (st.hasMoreTokens())
  9. {
  10. // your code here
  11. System.out.println("token: " + st.nextToken());
  12. }
  13.  
  14. }
  15. }
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Feb 23rd, 2007
0

Re: string tokenizer

Quote ...
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
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 23rd, 2007
0

Re: string tokenizer

I assume someone needs to tell the teacher that. LOL
I was wondering why one wouldn't just regex for validation.
Last edited by nikkiH; Feb 23rd, 2007 at 5:05 pm.
Reputation Points: 13
Solved Threads: 4
Junior Poster in Training
nikkiH is offline Offline
79 posts
since Dec 2006
Feb 24th, 2007
0

Re: string tokenizer

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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 26th, 2007
0

Re: string tokenizer

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.




Click to Expand / Collapse  Quote originally posted by mengani123 ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kashko is offline Offline
4 posts
since Feb 2007
Feb 26th, 2007
0

Re: string tokenizer

Click to Expand / Collapse  Quote originally posted by kashko ...
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how use integer values begins with zero
Next Thread in Java Forum Timeline: persisting the session





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC