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

标签 java loops while-loop filereader

我的问题是我必须编写代码来检查客户姓名是否已在我的 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/

相关文章:

java - 如何正确嵌套循环

laravel - Laravel Blade 循环中 undefined variable $loop

java - 第 (N-2) 和 (N-1) 的第 N 个输出和

java - 如何在 while 循环后正确初始化和使用变量

javascript - 如何防止 while 循环中最后一条语句被记录到控制台?

java - 为什么@PostConstruct 会导致NullPointerException?

r - 正确地将 for 循环转换为并行循环

java - 无法在 Java 上输入所需的名称

java - 访问包中另一个类中的 ArrayList 时遇到困难

java - Thread 类的静态 sleep 方法如何在不访问 'this' 引用的情况下工作?