Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,168 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,558 people online right now. Registration is fast and FREE... Join Now!




StringTokenizer

 

StringTokenizer, Problem with skipping over empty fields when reading a record

javamad

18 Sep, 2009 - 05:21 AM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2009
Posts: 65



Thanked: 1 times
My Contributions
HI,

I am trying to read a pipe (|) delimited file using the StringTokenizer. My problem is that when there is nothing between the pipes the StringTokenizer skips over the field (I would like to return nothing instead of skipping over to the next field.

eg if I have the folloing pipe delimited record
A|B|C
I would expect to get the result
String a = 'A'
String b ='B'
String c = 'C'

and for the following I would expect to see
A||C
String a = 'A'
String b = ''
String c = 'C'


But I am returning:
String a = 'A'
String b = 'C'
String c = ''

please see below for the code any help will be greatly appreciated

Thanks

CODE

public class RestrictedList {
    
    public RestrictedList (SQLConn Uconn,String Y, String M, String D){
        String address = "c:\\tmp\\restforb." + Y + M + D;
        String thisLine;
        String query = null;
        String column1Result;
        String column2Result;
        String countryId;
        
        query = "truncate table restricted_list";
        
        SQLQuery restListquery = new SQLQuery(Uconn, query);
        
        try {
            FileReader file = new FileReader(address);
            BufferedReader buff = new BufferedReader(file);
            String keyField = null;
            
            
            String symbol = "";
            String timestamp = "";
            String dateAdded = "";
            String timeAdded = "";
            String addedBy = "";
            String reason = "";
            String canBuy = "";
            String canSell = "";
            String mustBuy = "";
            String mustSell = "";
            String mustClear = "";
            String comment = "";
            String longCanBuy = "";
            String longCanSell = "";
            String longMustSell = "";
            String shortCanBuy = "";
            String shortCanSell = "";
            String ShortMustBuy = "";
            
            

            boolean eof = false;

            if (address.equals("c:\\tmp\\restforb." + Y + M + D)) {

                while ((thisLine = buff.readLine()) != null && !eof) {
                    StringTokenizer st = new StringTokenizer(thisLine, "|"); //think the problem is here but do not know what the syntax is to stop the loop skipping over the empty fields

                    while (st.hasMoreElements()) {

                        for (int i = 0; i < 18; i++) {
                            String field = st.nextToken();

                            switch (i) {

                            case 0:
                                // ric
                                keyField = field;
                                symbol = keyField;
                                break;
                            case 1:
                                // timestamp
                                new String(field);
                                timestamp = field;
                                break;
                            case 2:
                                // date added
                                new String(field);
                                dateAdded = field;
                                break;
                            case 3:
                                // Time added
                                new String(field);
                                timeAdded = field;
                                break;
                            case 4:
                                // added by (WQ or MLP)
                                new String(field);
                                addedBy = field;
                                break;
                            case 5:
                                // reason
                                new String(field);
                                reason = field;
                                break;
                            case 6:
                                // can buy
                                new String(field);
                                canBuy = field;
                                break;
                            case 7:
                                // can sell
                                new String(field);
                                canSell = field;
                                break;
                            case 8:
                                // must buy
                                new String(field);
                                mustBuy = field;
                                break;
                            case 9:
                                // must sell
                                new String(field);
                                mustSell = field;
                                break;
                            case 10:
                                // must buy to clear (obsolete)
                                new String(field);
                                mustClear = field;
                                break;
                            case 11:
                                // must sell to clear (obsolete)
                                new String(field);
                                comment = field;
                                break;
                            case 12:
                                //must clear by date
                                new String(field);
                                longCanBuy = field;
                                break;
                            case 13:
                                // comment
                                new String(field);
                                longCanSell = field;
                                break;
                            case 14:
                                // long can buy
                                new String(field);
                                longMustSell = field;
                                break;
                            case 15:
                                // long can sell
                                new String(field);
                                shortCanBuy = field;
                                break;
                            case 16:
                                // long must sell
                                new String(field);
                                shortCanSell = field;
                                break;
                            case 17:
                                // short can buy
                                new String(field);
                                ShortMustBuy = field;
                                break;
                            default:
                                System.out
                                        .println(new String(field)
                                                + "Too many data timeseries data types \t");
                            }

                        }

                        

                        query = "INSERT INTO restricted_list & #40;symbol,time_stamp,date,time_added,added_by,reason,can_buy,can_sell,must_buy,
must_sell,must_clear,comment,long_can_buy,long_can_sell,long_must_sell,short_can
_buy,short_can_sell,short_must_sell)" +
                                " VALUES ('" + symbol +"','"+timestamp +"','"+dateAdded +"',"+timeAdded +",'"+addedBy +"','"+reason +"',"+canBuy +","+canSell +","+mustBuy +","+mustSell +","+mustClear +",'"+comment +"',"+longCanBuy +","+longCanSell +","+longMustSell +","+shortCanBuy +","+shortCanSell +","+ShortMustBuy + ")";


                            
                            Uconn.execute(query);
                            

                        }

                    }

                

            } else {
                System.out
                        .println("File address "
                                + address
                                + " not correct, "
                                + "please make sure instrument file path is present and correct");

            }


        } catch (IOException e) {
            System.out.println("Error -- " + e.toString());
        }
        
        
    }

}




User is offlineProfile CardPM
+Quote Post

 
Reply to this topicStart new topic
Replies(1 - 4)

NoobKnight

RE: StringTokenizer

18 Sep, 2009 - 05:30 AM
Post #2

New D.I.C Head
*

Joined: 14 Jul, 2009
Posts: 45



Thanked: 4 times
My Contributions
CODE

        StringTokenizer st = new StringTokenizer(thisLine, "|");



Try... (thisLine, "| "); // put a space after the pipe. I don't have my compiler up so I can't try it here.
User is offlineProfile CardPM
+Quote Post

javamad

RE: StringTokenizer

18 Sep, 2009 - 05:40 AM
Post #3

D.I.C Head
**

Joined: 8 Jun, 2009
Posts: 65



Thanked: 1 times
My Contributions
nice try, but prob still there
User is offlineProfile CardPM
+Quote Post

javamad

RE: StringTokenizer

18 Sep, 2009 - 05:56 AM
Post #4

D.I.C Head
**

Joined: 8 Jun, 2009
Posts: 65



Thanked: 1 times
My Contributions
Hi,

I have found a solution by using the split function

thanks
User is offlineProfile CardPM
+Quote Post

pbl

RE: StringTokenizer

18 Sep, 2009 - 01:28 PM
Post #5

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
In the constructor of StringTokenizer() you can ask, with a boolean to return the delimiter
If you ask for the delimiter and the String length() is 1 it means you only have |
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 06:35PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month