首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >行的检查是在带有缓冲区读取和哈希集的文件中。

行的检查是在带有缓冲区读取和哈希集的文件中。
EN

Stack Overflow用户
提问于 2012-03-10 17:56:49
回答 2查看 368关注 0票数 0

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

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

我已经拥有的代码如下。

代码语言:javascript
复制
    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());
    }
    }

荷兰代码

代码语言:javascript
复制
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());}



    }


}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-10 19:02:05

我真的不确定您总体上想做什么(每4行读取一次),但是如果您只想检查文件中是否存在字符串,我就不会使用HashSet

下面是一个完整的程序,用于检查文件中是否存在字符串:

代码语言:javascript
复制
import java.io.*;

public class CheckName
{
  private static boolean doesNameExistInFile(String name, String filename)
    throws FileNotFoundException, IOException
  {
    BufferedReader reader = new BufferedReader(new FileReader(filename));

    String line = null;

    try {
      while ((line = reader.readLine()) != null) {
        if (line.equals(name))
          return true;
      }

    } finally {
      // Always close() before exiting.
      reader.close();
    }

    return false;
  }

  public static void main(String[] args)
    throws FileNotFoundException, IOException
  {
    boolean exists = doesNameExistInFile("Bender", "input.txt");

    System.out.println(exists ? "Exists!" : "Does not exist.");
  }
}

输入文件input.txt的内容

代码语言:javascript
复制
Leela
Fry
Professor
Hermes
Zoidberg

有几件事要注意:

  1. 我们不读取整个文件。一旦找到字符串,我们就停止阅读,因为这是我们感兴趣的所有内容。
  2. 总是在退出之前一直在close(),不管是否找到了字符串。我们将调用放在finally块中.
票数 0
EN

Stack Overflow用户

发布于 2012-03-10 18:28:15

这是:

代码语言:javascript
复制
    while ((line = br.readLine()) != null)
    {
        i++;
        if (i == 1){hs.add(br.readLine());}

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

    }

应该是这样的:

代码语言:javascript
复制
    while ((line = br.readLine()) != null)
    {
        i++;
        if (i == 1)
            hs.add(line);
        if (i % 4 == 0)
            hs.add(line);
    }

也就是说,您可能意味着add您刚刚读过的行,而不是在新行中阅读,并对其进行add处理。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9648803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档