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?