2012-03-10 24 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