Link to home
Start Free TrialLog in
Avatar of cHEo
cHEoFlag for Malaysia

asked on

String Tokenizer

I want to read from a text file which delimiters is tab and construct it as a SQL query string. I have problem when any of the field is empty.


StringTokenizer st = new StringTokenizer(thisLine, "\t"); while (st.hasMoreTokens()){
     query = query + "\'" + st.nextToken() + "\'" ;
        if(st.hasMoreTokens())
          query = query + "," ;
      // some other codes
}
mesg = "INSERT into Student_Result (STUID, AGE, SEX, RACE) "+  "values (" + query + ")";

If in the text file, any field is empty, the mesg string will be a problematic sql statement.

Thank you
Avatar of kotan
kotan
Flag of Malaysia image

Try to use this,

StringTokenizer st = new StringTokenizer(thisLine, "\t", true);

This may solve ur problem but need some additional code..
Avatar of cHEo

ASKER

i tried before to use StringTokenizer(thisLine, "\t", true) but is having problem also. Can u provide me the additional code? thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of raid999
raid999

I am not sure if the is what you want but check if the tokens length is 0 then do what ever you wanan do.
Avatar of cHEo

ASKER

Objects, I was the one who personally sent email to ask you about other java problem. You are just fantastic. Thank a lot.
Happy to help :-)

http://www.objects.com.au
Brainbench MVP for Java 1
http://www.brainbench.com