Solved

String Tokenizer

Posted on 2002-05-22
6
213 Views
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() + "\'" ;
        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
0
Comment
Question by:cHEo
6 Comments
 
LVL 6

Expert Comment

by:kotan
Comment Utility
Try to use this,

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

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

Author Comment

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

Accepted Solution

by:
objects earned 50 total points
Comment Utility
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 = "";
    }
    else
    {
      value = next;
    }
}
query += "\'" + value + "\'";

mesg = "INSERT into Student_Result (STUID, AGE, SEX, RACE) "+  "values (" + query + ")";
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 1

Expert Comment

by:raid999
Comment Utility
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.
0
 

Author Comment

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

Expert Comment

by:objects
Comment Utility
Happy to help :-)

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now