1

I have a text file I am trying to break up with string tokenizer. Here is a few lines of the text file:

Mary Smith 1 
James Johnson 2 
Patricia Williams 3 

I am trying to break up into first name, last name and Customer ID.

I have so far been able to do that but it stops after mary smith.

Here is my code:

  public static void createCustomerList(BufferedReader infileCust,
            CustomerList customerList) throws IOException
{    


            String  firstName;
            String  lastName;
            int  custId;


            //take first line of strings before breaking them up to first last and cust ID
            String StringToBreak = infileCust.readLine();
            //split up the string with string tokenizer
            StringTokenizer st = new StringTokenizer(StringToBreak);

            firstName = st.nextToken();

            while(st.hasMoreElements())
            {
            lastName =  st.nextToken();
            custId = Integer.parseInt(st.nextToken());
            CustomerElement CustomerObject = new CustomerElement();
            CustomerObject.setCustInfo(firstName,lastName,custId);
            customerList.addToList(CustomerObject);

            }


    }
helios
  • 13,574
  • 2
  • 45
  • 55
Sackling
  • 1,780
  • 5
  • 37
  • 71
  • 2
    I should use camelCase for every variable declaration. In your code `StringToBreak` and `CustomerObject` have initial capital letters, and that's reserved for types (classes and interfaces). It works but it leads to confusion. – helios Feb 13 '12 at 16:59

3 Answers3

3
String StringToBreak = infileCust.readLine();

reads the FIRST line from the file. And you feed the StringTokenizer with it. It's normal that StringTokenized doesn't find more tokens.

You have to create a second loop enclosing all this to read every line. It is:

outer loop: readLine until it gets null {
   create a StringTokenizer that consumes *current* line
   inner loop: nextToken until !hasMoreElements()
}

Well, indeed you don't need to do an inner loop because you have three different fields. It's enough with:

name = st.nextToken();
lastName = st.nextToken();
id = st.nextToken;
helios
  • 13,574
  • 2
  • 45
  • 55
  • Ok I get what you are saying completely but I am having trouble writing the outer loop though. My outter loop is pretty much just this: while (infileCust.readLine() != null) { – Sackling Feb 13 '12 at 17:34
  • What do you mean by consumes current line? – Sackling Feb 13 '12 at 18:48
  • Also at the end you say I don't need an inner loop so I am guessing I just need to tell the tokenizer to "consume" the line but I don't know the method and can't seem to find one.. – Sackling Feb 13 '12 at 18:53
1

For the outer loop, you need to store the contents of the current line in the stringToBreak variable so that you can access it inside the loop. You need a new StringTokenizer for each line so it needs to be inside the loop.

String stringToBreak = null;
while ((stringToBreak = infileCust.readLine()) != null) {
     //split up the string with string tokenizer
     StringTokenizer st = new StringTokenizer(stringToBreak);
     firstName = st.nextToken();
     lastName =  st.nextToken();
     custId = Integer.parseInt(st.nextToken());
}
AlanS
  • 61
  • 3
  • I have to add StringTokenizer st = new StringTokenizer(stringToBreak); right before the while loop as well no? I am getting an error: exception in thread "main" java.lang.NullPointerException – Sackling Feb 13 '12 at 19:02
  • Thanks for the edit. And this looks right but now it is just printing a blank line instead of the 1 – Sackling Feb 14 '12 at 00:17
0

First off, you want to look at your loop, specifically how you have firstName outside of the loop so that is going to throw all of you tokens off. You will be trying to create new customer objects without enough information.

Kenneth Funk
  • 396
  • 2
  • 12