gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:43:47 27 4
gpt4 key购买 nike

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();
}

}

最佳答案

在for循环中添加以下行

 StringTokenizer stok = new StringTokenizer(fs.nextLine(), ",|//");

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

关于循环到第二个客户端时出现 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29866866/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com