aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes StringTokenizer and flags Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "StringTokenizer and flags" Watch "StringTokenizer and flags" New topic
Author

StringTokenizer and flags

lphung
Greenhorn

Joined: Feb 12, 2001
Posts: 2
Hello folks, this may be a simple problem for some but I'm somewhat new to java and would really appreciate anyone who can supply some sample code for the following.
Basically, I am reading in a comma delimited file and mapping the fields (that are separted by the commas).
I think an example would be make this a bit simple.
For example. I am reading in "input.txt"
The contents of "input.txt" is as follows:
The first line of "input.txt" is a column header with 3 fields:
"1","2","3
The second line of "input.txt" is the actual data.
"orange","john","bob"
I am writing a utility that initially reads in the first line of input.txt, and figures out what the column header fields are. In my application, the column header actually has a maximum of FIVE fields ("1","2","3","4","5")
Here is what I'm doing so far.....
/*** BEGIN CODE (parts of my code) *******
String inputLine;
// reading the first line (header file)
// if field exists, flag it as "1", else flag it as "0"
inputLine = br.readLine();
StringTokenizer hd = new StringTokenizer(inputLine, ",");
String header[] = new String[hd.countTokens()];
String flag[] = new String[5];
for (int i = 0; hd.hasMoreElements(); i++)
{
header[i] = hd.nextToken();
if (header[i].equals("\"1\""))
{
flag[0] = "1";
}
else if (header[i].equals("\"2\""))
{
flag[1] = "1";
}
else if (header[i].equals("\"3\""))
{
flag[2] = "1";
}
else if (header[i].equals("\"4\""))
{
flag[3] = "1";
}
else if (header[i].equals("\"5\""))
{
flag[4] = "1";
}
else
{
flag[i] = "0";
}
**** END OF PARTIAL CODE **/
Here is my main question. There may be 100 input files. Each of them might have different numbers of column header fields. Some may be "1","2","3","4", another may be "1","3","4","5", etc.... How do I do my mapping according to what the column header field reads? If header "1","2","3","4","5" from input.txt maps to "A","B","C","D","E" in output.txt. Yet the header fields may be different for each input file. What is the simplest way to map it?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
Why not try something neat like using the numeric read in to index into some output array. Assumeing you know that the input will always be the String: "#".

Regards,
Manfred.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
"lphung",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp . We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please choose a new name which meets the requirements.
Thanks.

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringTokenizer and flags