1

I get a NoSuchElementException, now debugging this I noticed that the Car and Carmap are created properly and the values stored appropriately so I'm not sure exactly which next token the ST isn't seeing? Or whether it's stopping when is sees that there are no more tokens.

Thanks to any and all for input.

    Carmap = new HashMap<String,Car>();
    //Change file path accordingly
    File f = new File("C:\\XXX\\XXX\\XXX\\CarFaxDB.txt");
    //Check to see if file exists, else create file
    if (f.exists()){
        String data[] = readFile(f);
        for (int i =0; i<data.length; i++){

            if (data[i] != null){
                if (i>0){
                    String line = data[i];
                    StringTokenizer st = new StringTokenizer(line,",");
                    String VIN = st.nextToken();
                    String carMake = st.nextToken();
                    String carModel = st.nextToken();
                    int carYear = Integer.parseInt(st.nextToken());
                    data[i]= line;
                    Car car = new Car(VIN, carMake, carModel, carYear);
                    Carmap.put(car.getVIN(), car);
                }
            }
        }

    }

2 Answers2

3

The error is coming because you are trying expecting a nextToken() but tokenizer has no more tokens.

You should check if you have more token before doing nextToken(). You can use hasMoreTokens() method to do it.

Also, you should check whether you get a non-empty line and start printing it to see whether it has all the tokens you are expecting.

Here is the corrected code snippet:

Carmap = new HashMap<String,Car>();

//Change file path accordingly
File f = new File("C:\\Users\\XXX\\Documents\\CarFaxDB.txt");

//Check to see if file exists, else create file
if (f.exists()){
    String data[] = readFile(f);
    for (int i = 0; i < data.length; i++){

        if (data[i] != null){
            if (i > 0){
                String line = data[i];

                if(!StringUtils.isEmpty(line)) {
                    System.out.println(line);
                    StringTokenizer st = new StringTokenizer(line,",");

                    /* Check For More Tokens */
                    String VIN = st.hasMoreTokens() ? st.nextToken() : null;
                    /* Check For More Tokens */
                    String carMake = st.hasMoreTokens() ? st.nextToken() : null;
                    /* Check For More Tokens */
                    String carModel = st.hasMoreTokens() ? st.nextToken() : null;
                    /* Check For More Tokens */
                    int carYear = st.hasMoreTokens() ? Integer.parseInt(st.nextToken()) : 0;
                    data[i] = line;

                    Car car = new Car(VIN, carMake, carModel, carYear);
                    Carmap.put(car.getVIN(), car);
                }
            }
        }
    }
}
user2004685
  • 9,548
  • 5
  • 37
  • 54
  • The only problem with this is the parseInt() can't accept st.hasMoreTokens() ? st.nextToken() : 0. Thank you though for your code snippet. –  Mar 09 '16 at 05:39
  • Ah. My bad, stupid copy-paste. Updated the solution. – user2004685 Mar 09 '16 at 05:42
-1

You have to check if the StringTokenizer has more tokens before calling StringTokenizer.nextToken(). You do it like this:

Carmap = new HashMap<String,Car>();
//Change file path accordingly
File f = new File("C:\\Users\\XXX\\Documents\\CarFaxDB.txt");
//Check to see if file exists, else create file
if (f.exists()){
    String data[] = readFile(f);
    for (int i =0; i<data.length; i++){

        if (data[i] != null){
            if (i>0){
                  String VIN, carMake, carModel = null;
                  int carYear = 0;
                  String line = data[i];
                  StringTokenizer st = new StringTokenizer(line,",");
                  if(st.hasMoreTokens()) {
                    VIN = st.nextToken();
                  if(st.hasMoreTokens())
                    carMake = st.nextToken();
                  if(st.hasMoreTokens())
                    carModel = st.nextToken();
                  if(st.hasMoreTokens())
                    carYear = Integer.parseInt(st.nextToken());
                  data[i]= line;
                  if(VIN != null && carMake != null && carModel != null && carYear > 0)
                  Car car = new Car(VIN, carMake, carModel, carYear);
                  Carmap.put(car.getVIN(), car);
                }
            }
        }
    }

}

please see the answer for this question it explains more.

Community
  • 1
  • 1
Armando
  • 459
  • 1
  • 8
  • 22
  • 1
    After `while(st.hasMoreTokens())`, `String VIN = st.nextToken();` will be okay but what about `String carMake = st.nextToken();`? You should not use a while loop but check it on every statement. – user2004685 Mar 09 '16 at 05:40
  • Oh, I did not see those ones. I guess you have to check if there are more tokens every time you want to call StringTokenizer.nextToken(). See, throwing NoSuchElementException is the documented behaviour if no more tokens are available, so you don't have alternative. It won't break your code though. – Armando Mar 09 '16 at 05:45
  • *I guess you have to check if there are more tokens every time you want to call StringTokenizer.nextToken().* Yes, you are right but what you suggested right now will break the code. – user2004685 Mar 09 '16 at 05:51
  • Not if you do it properly. I guess you can only create a new car if you have VIN, carMake, carModel and carYear, but if you can not get them all from the StringTokennizer then you can not create the car. – Armando Mar 09 '16 at 05:52