循环到第二个客户端时java.util.NoSuchElementException

时间:2015-04-25 15:05:41

标签: java arrays stringtokenizer nosuchelementexception

java的新手,我要创建一个包含5个客户端对象和pet对象的数组,具体取决于指定文件中的数量。然而,它读取第一个客户端和宠物信息罚款,但当我到达下一个客户端时,它给了我 java.util.NoSuchElementException:null(在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();
                    } 

                }      

1 个答案:

答案 0 :(得分:2)

在for循环中添加以下行

std::wstring

注意:首先从当前位置删除上一行