使用FileReader检查txt文件中是否已经存在客户名称 [英] Checking if a customers name is already in a txt file with FileReader

查看:101
本文介绍了使用FileReader检查txt文件中是否已经存在客户名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is that I have to write code to check if a customers name is already in my txt file (Customers.txt).

如您所见,我在txt文件的第四行中写下我的客户名称. 我想使用SimpleInOutDialog来输入客户名称,然后将输入内容与我的txt文件中的所有客户名称进行比较. 我该怎么做?

As you can see, I write my customers name ,every fourth line in my txt File. I would like to have the input of a customers name with SimpleInOutDialog en then that inputs needs to be compared with al the customers name in my txt file. How do I need to do this?

下面已经有我的代码.

//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()中 谢谢!

The changes need to be in be in getCustomer() Thanks!!

解决了我的问题,这是解决方法:

Hi , Solved my problem , here is the solution:

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方法查看客户是否已经存在.如果名称存储在第四行,则代码应如下所示

Use a HashSet to store all of the names you read and then it's just to call the contains methode to see if the customer already exists. If the names are stored on the forth line the code should look like this

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

       }
}

这篇关于使用FileReader检查txt文件中是否已经存在客户名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆