2012-03-10 55 views
0

我的问题是,我必须编写代码来检查客户名称是否已经在我的txt文件(Customers.txt)中。检查一行是在缓冲读取和哈希集文件

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

我已经拥有的代码如下。

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

Dutcht代码

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



    } 


} 
+1

如果您使用更常规的命名方式(并为您的变量赋予了有意义的名称)并将其合理格式化,那么您的代码将更容易阅读。 – 2012-03-10 18:03:31

+0

你应该发布你的实际代码。荷兰语变量名称并不理想,但它们比破碎的代码更好。 (例如,“创建新客户”在Java中不是一个有效的方法名称。) – ruakh 2012-03-10 18:22:37

+0

@ruakh我会更新它。 – PauloPawelo 2012-03-10 18:25:41

回答

0

我真的不确定你想要做什么总体(阅读每4行等),但我不会使用HashSet如果你想要的只是检查一个字符串中是否存在一个文件。

这里有一个完整的计划,检查是否在文件中存在的字符串:

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的内容:

Leela 
Fry 
Professor 
Hermes 
Zoidberg 

几件事情需要注意:

  1. 我们不读整个文件。我们一旦找到字符串就会停止阅读,因为这是我们感兴趣的全部内容。
  2. 退出前始终保持close(),是否找到字符串。我们将电话拨入finally区块。
+0

奥克非常感谢!现在必须考虑我将如何使用这个课程,所以我可以要求它... – PauloPawelo 2012-03-10 19:25:14

1

此:

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

也就是说—你可能的意思是add你刚读过的行,而不是在新行行和add行。

+0

此外,似乎它读取第1,4,8行等,第一行是正确的,但其余的读取客户编号。两者都应该被替换为'if(i%4 == 1)'。 – 2012-03-10 18:38:02

+0

@Joachim Isaksson那么代码中需要改变什么?或者你给的代码是正确的? – PauloPawelo 2012-03-10 18:45:35

+0

@NB。应该是,尽管你总是可以在ruakh的代码中打印出'line'来查看你是否获得了与之相比较的正确内容。 – 2012-03-10 18:48:16