?
Solved

Reading comma delimited files

Posted on 2005-04-24
6
Medium Priority
?
3,346 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:ramani081399
  • 4
6 Comments
 
LVL 3

Expert Comment

by:RomanRega
ID: 13853283
String parts[]=string.split(",");
0
 
LVL 3

Expert Comment

by:RomanRega
ID: 13853310
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
 
LVL 3

Expert Comment

by:RomanRega
ID: 13853316
ehm: never mind the  "In fact" thing in the last line :)
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.

 

Author Comment

by:ramani081399
ID: 13855734
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
 
LVL 16

Expert Comment

by:gnoon
ID: 13856341
Hi,

StringTokenizer will not returns null, but empty string.
You can simply check it before, then add null to the hashmap instead.
0
 
LVL 3

Accepted Solution

by:
RomanRega earned 200 total points
ID: 13857232
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

Featured Post

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.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 19 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