String Tokenizer

Posted on 2002-05-22
Last Modified: 2010-03-31
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
Question by:cHEo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7028643
Try to use this,

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

This may solve ur problem but need some additional code..

Author Comment

ID: 7028673
i tried before to use StringTokenizer(thisLine, "\t", true) but is having problem also. Can u provide me the additional code? thanks
LVL 92

Accepted Solution

objects earned 50 total points
ID: 7028684
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 + ")";
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Expert Comment

ID: 7028685
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.

Author Comment

ID: 7028829
Objects, I was the one who personally sent email to ask you about other java problem. You are just fantastic. Thank a lot.
LVL 92

Expert Comment

ID: 7028843
Happy to help :-)
Brainbench MVP for Java 1

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question