samedi 25 avril 2015

java.util.NoSuchElementException when looping to the second client


new to java and I'm tring to create an array of 5 client objects and pet object depending on the amount in the file specified. However, it reads the first client and pet info fine but when I get to the next client it gives me java.util.NoSuchElementException: null (in java.util.StringTokenizer).

import java.io.*;
import java.util.*;
/**
 * Write a description of class VetClientDriver here.
 * 
 * @author (Anastasia) 
 * @version (4/19/15 --THPT4)
 */
public class VetClientDriver
{
    /**
     * Constructor for objects of class VetClientDriver
     */
    public static void main(String[] args) throws IOException
    {
        String filename = ("C:\\Users\\Anastasia\\Desktop\\clientdata.txt") ;
        File file = new File(filename);
        Scanner fs = new Scanner(file);
        StringTokenizer stok = new StringTokenizer(fs.nextLine(), ",|//");

        VetClient []clients;
        VetPets []pets;
        String last, first, addr, id, num, balance;
        String name,type,rabies,visit,weight;
        String numOfPets;
        //fs = new Scanner(file);
         //stok = new StringTokenizer(fs.nextLine(), ",|//");
         clients = new VetClient[5];
          for(int i=0; i<clients.length; i++)
                     {

                     last = stok.nextToken();
                     first = stok.nextToken();
                     addr = stok.nextToken();
                     id = stok.nextToken();
                     num = stok.nextToken();
                     balance = stok.nextToken();
                     numOfPets = stok.nextToken();

                     clients[i] = new VetClient(last,first,addr,id,num,balance,numOfPets);
                     System.out.println(clients[i].clientInfo());
                     pets = new VetPets[Integer.parseInt(numOfPets)];

                     for(int k=0; k<pets.length; k++)
                        { 
                       stok = new StringTokenizer(fs.nextLine(), ",|/");
                       name = stok.nextToken();
                       type = stok.nextToken();
                       weight = stok.nextToken();
                       rabies = stok.nextToken();
                       visit = stok.nextToken();
                       pets[k] = new VetPets(name,type,weight,rabies,visit);
                       System.out.println(pets[k].petInfo() + "\n");

                        }  


                    }
                     fs.close();
                    } 

                }      


Aucun commentaire:

Enregistrer un commentaire