java 的新手,我试图根据指定文件中的数量创建一个包含 5 个客户端对象和宠物对象的数组。但是,它可以很好地读取第一个客户端和宠物信息,但是当我到达下一个客户端时,它给了我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();
}
}
在 for 循环中添加以下行
StringTokenizer stok = new StringTokenizer(fs.nextLine(), ",|//");
注意:首先从当前位置删除上述行