• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

StringTokenizer and flags

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
reply
    Bookmark Topic Watch Topic
  • New Topic