aspose file tools*
The moose likes I/O and Streams and the fly likes Reading a tab delimited txt file and replacing blanks with zeores. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading a tab delimited txt file and replacing blanks with zeores." Watch "Reading a tab delimited txt file and replacing blanks with zeores." New topic
Author

Reading a tab delimited txt file and replacing blanks with zeores.

Shalini Sridhar
Greenhorn

Joined: May 20, 2006
Posts: 4
Hi,
I am trying to open a tab delimited file with 4 columns and corresponding values under it. If no value under the column, 0 should be replaced. I used the StringTokenizer and the split methods, but this doesnt seem to work. Any ideas?
E.g
Title1 Title2 Title3
4 7
5 2

SHould be like this:
Title1 Title2 Title3
4 0 7
0 5 2

This has been breaking my head. Actually, I dont have problems with code. I have kept writing and rewritg, trying various different methods. Instead of pasting my very messy code, i just gave the logic.

I used Buffered reader to read the file.
But here is a part of it - It should eventualy write to another file.This was just to use the StringTokenizer to retun me the number of tokens, including tabs.

record = new String(); //Sring to hold each record
BufferedWriter output = new BufferedWriter(new FileWriter("file"));

while ((record = br.readLine())!= null) //To read from file line by line
{

StringTokenizer st = new StringTokenizer(record, "\t", true);
int numTokens = st.countTokens();
String [] token = new String [numTokens];

for (int i = 0; i<numTokens ; i++)
{
token = st.nextToken("\t");
}
}

---------------------------------------------------------------------------I am just trying to figure out the logic for it. I need to be able to identfy a missing value under a heading. If the columns are delimted by tabs, how do I recognize the difference between a tab and an empty space?

Methods I Used
I tried using split, but ofcourse, it dint tell me where the tabs are. It just split the words separated by tabs.

I then used String Tokenizer and used the boolean delims as true, so the tabs were taken as tokens as well, but this doesnt help coz, I am stuck there. A value can be missing under any column. So how would I need to recognize that?

Help please!

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Why do you not try with java.io.StreamTokenizer or if you are using JDK 1.5 you may consider java.util.Scanner.

Maybe this way it could be a little easier.
Shalini Sridhar
Greenhorn

Joined: May 20, 2006
Posts: 4
Thanks for your reply.
Actually i used split method and put the string into an array. It also stores the tab values. I just go through the array, replace empty strings with 0 and concatenate the string back with tabs.
How ever, the trick now is, I also need to delete an entire row, if only the last column value is empty.
Any suggestions?
[ May 20, 2006: Message edited by: Shalini Sridhar ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a tab delimited txt file and replacing blanks with zeores.