2012-03-10 6 views
0

私の問題は、顧客名がすでに自分のtxtファイル(Customers.txt)に入っているかどうかをチェックするコードを書く必要があることです。バッファリングされたハッシュセットのファイルに行のチェックがあります

問題は、お客様がファイルに含まれていて、ファイルの中にファイルが存在しないことを確認した場合です(手動でチェックしました)。 解決してください。

私がすでに持っているコードは以下の通りです。

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

従来の命名法を使用して変数に意味のある名前を付けるとコードが読みやすく*分かりやすくなりました。 –

+0

実際のコードを掲載する必要があります。オランダ語の変数名は理想的ではありませんが、壊れたコードよりも優れています。 (例えば、 '新しい顧客を作る'は、Javaでは有効なメソッド名ではありません。) – ruakh

+0

@ruakhそれを更新します。 – PauloPawelo

答えて

0

全体的に何をしようとしているのかは分かりませんが(4行目ごとに読むなど)、文字列がファイルに存在するかどうかを確認するだけの場合はHashSetを使用しません。

はここで文字列がファイル内に存在するかどうかをチェックするための完全なプログラムです:入力ファイルinput.txt

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

内容:

Leela 
Fry 
Professor 
Hermes 
Zoidberg 

注意すべき物事のカップル:

  1. ファイル全体を読み取るわけではありません。
  2. 文字列が見つかったかどうかにかかわらず、終了する前に必ずclose()と入力してください。私たちはfinallyブロックに電話をかけました。
+0

よろしくお願いします!今、このクラスをメソッドで使う方法を考えなければならないので、私はそれを呼び出すことができます... – PauloPawelo

1

は、この:ある

while ((line = br.readLine()) != null) 
    { 
     i++; 
     if (i == 1) 
      hs.add(line); 
     if (i % 4 == 0) 
      hs.add(line); 
    } 

while ((line = br.readLine()) != null) 
    { 
     i++; 
     if (i == 1){hs.add(br.readLine());} 

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

    } 

は、おそらくこのことになっていますあなたはたぶんaddに、の新しいの行とaddの行を読み込むのではなく、ちょうど読み込んだ行を意味していました。

+0

また、1行目、4行目、8行目、1行目は正しいと思われますが、残りは顧客番号を読み込んでいるようです。どちらもおそらく 'if(i%4 == 1) 'に置き換えてください。 –

+0

@ Joachim Isakssonコード内で何を変更する必要がありますか?またはあなたが正しかったコードですか? – PauloPawelo

+0

@NB。あなたが正しいコンテンツを取得しているかどうかを確認するために、ruakhのコードに 'line'をいつも出力することはできますが、それは正しいはずです。 –

関連する問題