?
Solved

String Tokenizer

Posted on 2002-05-22
6
Medium Priority
?
222 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
ID: 7028643
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
ID: 7028673
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 200 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 = "";
    }
    else
    {
      value = next;
    }
}
query += "\'" + value + "\'";

mesg = "INSERT into Student_Result (STUID, AGE, SEX, RACE) "+  "values (" + query + ")";
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Expert Comment

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

Author Comment

by:cHEo
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 7028843
Happy to help :-)

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 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