gpt4 book ai didi

java - 使用 FileReader 检查客户姓名是否已存在于 txt 文件中

转载 作者:行者123 更新时间:2023-12-02 07:56:16 25 4
gpt4 key购买 nike

我的问题是我必须编写代码来检查客户姓名是否已在我的 txt 文件 (Customers.txt) 中。

如您所见,我在 txt 文件中每隔四行写入我的客户姓名。我希望使用 SimpleInOutDialog en 输入客户姓名,然后需要将输入与我的 txt 文件中的所有客户姓名进行比较。我需要怎样做?

我已经拥有的代码如下。

//make SimpleInOutDialog   
SimpleInOutDialog input = new SimpleInOutDialog("Customers");
namecustomer = input.readString("Give in the name");
try{
BufferedReader br = new BufferedReader(new FileReader("L:\\Documents/Informatica/6de jaar/GIP/klanten.txt"));
HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;

if (i % 4 == 0){
if(hs.contains(namecustomer)){
//customer exists
input.showString("The customer exists", "");}
else
{setNewcustomer();}}


}
}catch (Exception e){//Catch wanneer er errors zijn
System.err.println("Error: " + e.getMessage());}
return line;
}



public void setNewcustomer(){
// make a newSimpleInOutDialog
SimpleInOutDialog input = new SimpleInOutDialog("A new customer");
//input
S = "Name customer: " + input.readString("Give in your name:");
WriteToFile();
S = "Adress: " + input.readString("Give your adress");
WriteToFile();
S = "Telephonenummber: " + input.readString("Give your telephonenumber");
WriteToFile();
//making a customerID
UUID idCustomer = UUID.randomUUID();
S = "CustomerID: " + customerID.toString();
WriteToFile();

}

public void WriteToFile(){
try{

FileWriter writer = new FileWriter("L:\\Documents/Informatica/6de jaar/GIP/Customer.txt", true);
BufferedWriter out = new BufferedWriter(writer);
//Wrting away your data
out.write(S);

//Closing the writer
out.close();


}catch (Exception e){//Catch when there are errors
System.err.println("Error: " + e.getMessage());
}
}

更改需要在 getCustomer() 中进行谢谢!!

您好,解决了我的问题,解决方案如下:

public void getKlant() {
// SimpleInOutDialog aanmaken
SimpleInOutDialog input = new SimpleInOutDialog("Klanten");
naamklant = input.readString("Geef de volledige naam in");
try{
BufferedReader br = new BufferedReader(new FileReader("L:\\Documents/Informatica/6de jaar/GIP/klanten.txt"));
HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i == 1){hs.add(br.readLine());}

if (i % 4 == 0){hs.add(br.readLine());}

}
if(hs.contains(naamklant)){
//klant bestaat
input.showString("De klant bestaat", "");
}else{setNieuweKlant();}


}catch (Exception e){//Catch wanneer er errors zijn
System.err.println("Error: " + e.getMessage());}

}

最佳答案

使用 HashSet存储您读取的所有名称,然后只需调用 contains 方法来查看客户是否已经存在。如果名称存储在第四行,则代码应如下所示

HashSet<String> hs = new HashSet<String>();
int i = 0;
while ((line = br.readLine()) != null)
{
i++;
if (i % 4 == 0)
{
if(hs.contains(line))
//name already exist
else
hs.add(line);
// new customer do what you want with it

}
}

关于java - 使用 FileReader 检查客户姓名是否已存在于 txt 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9589977/

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