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

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 + ")";
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse argument 14 60
check java version using powershell 13 86
Java exception bubble up 2 17
Android development question 2 36
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

776 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