检查客户的名称是否已在FileReader的文本文件中

时间:2012-03-06 18:50:04

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

}

1 个答案:

答案 0 :(得分:2)

使用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

       }
}