Reading comma delimited files

I am trying toi read a comma delimited file which looks like
RR,R,Y,N,,,,S,T and several such rows  from a text file.
I am using a StringTokeniser with , as the delimiter to read each token and put each token in a Hashtable.I would like to be able to check if there is nothing between two commas then it assign a null value for that item?? How do I this. Currently when i check for nextToken and put in the hashtable it does not take the null between two commas.
I would appreciate if some solution is possible with a snippet of the code. We could use Hashmap if Hashtable does not permit null values.
Thanks
Ramani
ramani081399Asked:
Who is Participating?
 
RomanRegaCommented:
Hi, Ramani.

You dont need to insert nulls at all.

If fact, for example, if you do:
     

      String  s= "RR,R,Y,N,,,,S,T";
      String p[]=s.split(",");
      for (int i=0; i<p.length; i++) {
               if (p[i].length()==0){
                     //you are between two commas
                    //do nothing(DO NOT insert nulls)
               }else{
                     //insert in the table or whatever you want
                    String key="fieldname"+i;
                    yourhashmap.put(key, p[i]);
               }
      }

You get exacly what you want. Notice that you dont have to insert nulls at all.

In fact if you do:

String field=(String)yourhashmap.get("fieldname5");

if (field==null){
     //fieldname5 is not present (has not been inserted yet, or is associated to null)
}else{
     //fieldname5 is present
}

The call of hashmap.get(key) (or hashmap.remove(key)) return null in two cases:
- The key has been associeted with a null value (doing like this: hashmap.put("key", null);
- The key has not been inserted at all.

So you can either do, as the first case above, and put nulls associating them with keys.
Or you can also do, as in the second cse above, and simply do not put anything would be null.

Either way you choose, calling:
String field=(String)yourhashmap.get("fieldname5");
will return a null if fieldname5 that have nothing but null to say :)
 

>> StringTokenizer will not returns null, but empty string

If I remember correcty, that's not entirely accurate: StringTokenizer does skip empty string. Not exacly what we are looking for here i think.

0
 
RomanRegaCommented:
String parts[]=string.split(",");
0
 
RomanRegaCommented:
Maybe is best if i explain:

if you do:

      String  s= "RR,R,Y,N,,,,S,T";
      String p[]=s.split(",");
      for (int i=0; i<p.length; i++) {
               if (p[i].length()==0){
                     //you are between two commas
               }else{
                     //insert in the table or whatever you want
               }
      }

May I ask why you want to insert null value in the table??

In fact
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
RomanRegaCommented:
ehm: never mind the  "In fact" thing in the last line :)
0
 
ramani081399Author Commented:
Hi,
  If I encounter a null I want to be able to put that in a collection preferably that which can be retrieved by a key. The reason for detecting the nulls is we I am not sure that always the input file is going to be without nulls. For example the line that has the String RR,R,Y,N,,,,S,T has to be mapped to a specific field name .So RR will map to fieldname1, the next R will map to fieldname2 etc. This when I put in a hashtable would be hashtable.put(fieldname1,RR),hashtable.put(fieldname2,R) etc. Now this hastable gets passed to other function calls and and in those functions I need to check with a fieldname. Say if filedname3 != null then do soemthing.
I hope that explains the need for detecting the nulls.
Now I need to be able to put them in  some collection and retrieve them??
Ramani
0
 
gnoonCommented:
Hi,

StringTokenizer will not returns null, but empty string.
You can simply check it before, then add null to the hashmap instead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.