• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

StringTokenizer and flags

Posts: 2
  • Mark post as helpful
  • send pies
  • 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:
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?
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: "#".

Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
The fastest and most reliable components of any system are those that are not there. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic