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() + "\'" ;
          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
Who is Participating?
objectsConnect With a Mentor Commented:
Try something like:

String value = "";
StringTokenizer st = new StringTokenizer(thisLine, "\t", true); while (st.hasMoreTokens()){
    String next = st.nextToken();
    if (next.equals("\t"))
      query = query + "\'" + value + "\'," ;
      value = "";
      value = next;
query += "\'" + value + "\'";

mesg = "INSERT into Student_Result (STUID, AGE, SEX, RACE) "+  "values (" + query + ")";
Try to use this,

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

This may solve ur problem but need some additional code..
cHEoAuthor Commented:
i tried before to use StringTokenizer(thisLine, "\t", true) but is having problem also. Can u provide me the additional code? thanks
7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

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.
cHEoAuthor Commented:
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 :-)
Brainbench MVP for Java 1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.