java - StringTokenizer issues with if statements -


i have tokenize string looks this:
4830673048;tony white
there must 2 tokens separated ;
first token must contain 10 digits, , digits
second token may not contain digits.

  private static boolean isvalid(string accountline) throws bankaccountexception   {      stringtokenizer strtok = new stringtokenizer(accountline, ";");      boolean valid = true;      if(strtok.counttokens() == 2)      {         if(strtok.nexttoken().length() == 10 && strtok.nexttoken().matches(".*[0-9].*"))         {            if(!strtok.nexttoken().matches(".*[0-9].*"))            {               valid = true;            }         }      }      else      {         system.out.println("invalid bank account info. " + strtok.nexttoken());         valid = false;      }      return valid;   } 

here code came with, doesn't expected do. know problem lies in use of .nexttoken(). question is, what's proper stringtokenizer method checking first or second token?

see if works you:

private static boolean isvalid(string accountline) throws bankaccountexception {    stringtokenizer strtok = new stringtokenizer(accountline, ";");    boolean valid = true;    if(strtok.counttokens() == 2)    {       string acctnum = strtok.nexttoken();       string acctholder = strtok.nexttoken();       if(acctnum.length() == 10          && acctnum.matches(".*[0-9].*")          && !acctholder.matches(".*[0-9].*"))       {          valid = true;        }    }    else    {       system.out.println("invalid bank account info. " + strtok.nexttoken());       valid = false;    }    return valid; } 

in code posted, calling nexttoken 2 times while evaluating first token, inadvertently moving on second token soon. assigning values variables first, can eliminate issue.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -