gpt4 book ai didi

java - 使用 bufferedread 和 hashset 检查文件中的行

转载 作者:行者123 更新时间:2023-12-01 15:30:02 24 4
gpt4 key购买 nike

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

问题是我用哈希集检查客户是否在文件中,而他说他不在文件中。(手动检查)请帮我解决这个问题。

我已经拥有的代码如下。

    public class Customer {
//declaration
public static String S;
private String line ;
public String nameCustomer;


/**
* constructor
*/
public Klant(){}

/**Checking of the customer is in the file
*/
public void getCustomer() {
// 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/Customers.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(nameCustomer)){
//the customer exists
input.showString("The customer exists", "");
}else{input.showString("The customer does not exist, we will make a new one", "");
setNieuweKlant();}


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

}


/**
* make a new customer
*/

public void Make new customer(){
// make SimpleInOutDialog
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();

}


/**
* Schrijft de gegevens weg naar het bestand
*/


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

荷兰代码

public class Klant {
//declaratie van de variabele die de tekst voorsteld
public static String S;
private String line ;
public String naamklant;


/**
* constructor
*/
public Klant(){}

/**Controleerd of de klant al bestaat
*/
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{input.showString("De klant bestaat niet, er wordt een nieuwe klant aangemaakt", "");
setNieuweKlant();}


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

}


/**
* Maakt een nieuwe klant aan
*/

public void setNieuweKlant(){
// SimpleInOutDialog aanmaken
SimpleInOutDialog input = new SimpleInOutDialog("Een nieuwe klant");
//input
S = input.readString("Geef de volledige naam in");
WriteToFile();
S = "Adres: " + input.readString("Geef het adres op");
WriteToFile();
S = "Telefoonummer: " + input.readString("Geef het telefoonnummer op");
WriteToFile();
//een klantennummer aanmaken
UUID idKlant = UUID.randomUUID();
S = "Klantnummer: " + idKlant.toString();
WriteToFile();

}


/**
* Schrijft de gegevens weg naar het bestand
*/
public void WriteToFile(){

try{

FileWriter writer = new FileWriter("L:\\Documents/Informatica/6de jaar/GIP/Klanten.txt", true);
BufferedWriter out = new BufferedWriter(writer);
//uw gegevens wegschrijven
out.write(S);
out.newLine();
//de writer sluiten
out.close();


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



}


}

最佳答案

这个:

    while ((line = br.readLine()) != null)
{
i++;
if (i == 1){hs.add(br.readLine());}

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

}

可能应该是这样的:

    while ((line = br.readLine()) != null)
{
i++;
if (i == 1)
hs.add(line);
if (i % 4 == 0)
hs.add(line);
}

也就是说,您可能想添加刚刚读过的行,而不是读取行并添加它。

关于java - 使用 bufferedread 和 hashset 检查文件中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648803/

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