permaculture playing cards
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

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "StringTokenizer and flags" Watch "StringTokenizer and flags" New topic

StringTokenizer and flags


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:
The second line of "input.txt" is the actual data.
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";
flag[i] = "0";
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
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: "#".

Frank Carver

Joined: Jan 07, 1999
Posts: 6920
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at . 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.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
I agree. Here's the link:
subject: StringTokenizer and flags
jQuery in Action, 3rd edition