String Tokenizer

hi
guys
what i want to do is to transfer  contents of file
which is in the formate
aaa:@:xxx:yyy
bbb:@:zzz:1.2.3.4:1.2.3.4-3.5.6.4
ccc:@:hhh:1.2.3.4:1.2.3.4-3.5.6.4:bullseye
etc
also i have to do some validations on individual tokens
like if toiken is ip address as shown
then get name of server append it to buffer
that's ok

so what i am doing is using a stream tokenizer
but the problem is when
i say
while(tokenizer.hasMoreTokens())
{
      Buffer.append(tokenizer.nextToken())
      name = tokenizer.nextToken()
      //validations like this
      if(name.equals("Amit"))
      {
            Buffer.append(name,"BE(ELECTRONICS)");
      }
      Buffer.append(tokenizer.nextToken());
      Buffer.append(tokenizer.nextToken());
}
what happens is for the first line  there is no ip address
hense while loop can't get terminated it gives exception
as no such element exists
what i need is that  loop shuld be terminated if there r no tokens
can any one help me
Argent
---Amit
amitdAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rjackmanConnect With a Mentor Commented:
hi
seems that
the problem lies with the .nextToken()
so why don't u retrieve tokens in the
vector
as
while(tokens.hasMOreElements())
{
  Vector V = token.NextToken()

}
then use it for validations
0
 
comermCommented:
Since the format of each line is variable, you need to model this as a state machine, e.g.

int state = 1;
while (tokenizer.hasMoreTokens())
{
   switch(state)
   {
      case 1:
         // we are expecting an "aaa"
         // type token.
         // if we get it, process it
         // and set state to 2...
         // if we don't get it,
         // set state equal to
         // something else...
         break;
      case 2:
         // etc.
         break;
      // etc
   }
}

When doing something like this, you need to map out your process using a state diagram. (BTW, there are better/cleaner ways to build a state machine, but the above is clear and easy to understand...)
0
 
amitdAuthor Commented:
this doesn'twork
0
 
amitdAuthor Commented:
thanx it works
0
All Courses

From novice to tech pro — start learning today.