2012-03-06 13 views
0

私の問題はです。お客様の名前がす​​でに自分のtxtファイル(Customers.txt)にあるかどうかを確認するコードを書く必要があります。お客様の名前が既にFileReaderのtxtファイルに入っているかどうかを確認

ご覧のとおり、お客様の名前、4行ごとにtxtファイルに書きます。 SimpleInOutDialogで顧客名を入力したいのですが、txtファイルの顧客名と入力を比較する必要があります。 どうすればいいですか?

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

//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に() 感謝する必要があります!

こんにちは、ここでは、私の問題を解決ソリューションです:

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

} 
+0

これまでに何を達成しようとしましたか?さらに、自分の脳を使わずに自分のプログラムを開発し続けようとしていたことを誇張せずに、自分のプログラムを開発し続けるよう求めている人が好きではありません。 - > http://stackoverflow.com/questions/9589689/make-filereader-read-every-fourth-line-with-a-loop –

+0

@AlbertoSolanoさて、私は2日間これについて考えていた、私は本当にドンBufferedReaderの仕組みについては理解できませんが、私のプログラムのベースであるため、この問題を解決する必要があります。私はすでに彼が望むサービスの種類、予約の時間、割引を得ることができるかどうかを選択させるための他のコードを持っています。私は皆さんに私のプログラムを開発させるつもりはないので、プログラムのこの部分について助けを求めるのは、それを動作させる方法がわからないためです。とにかく私の質問を確認するためにありがとう。 – PauloPawelo

答えて

2

使用HashSetあなたが読ん名前のすべてを保存すると、それは、顧客が既に存在するかどうかを確認するためにMethodeのが含まれているコールするだけです。名前が4行目に格納されている場合、コードは次のようになります

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 

     } 
} 
+0

私はコードを編集したようですか?私はこの方法で試してみたので、顧客が存在していない間は存在していないと言っていました。 – PauloPawelo

+0

このコードは新しい顧客を作成しません。単に名前を読み取り、その名前が既に存在するかどうかを確認します。 else文の下で新しいユーザを作成する必要があります – nist

関連する問題