0

私の問題は、File Reader で顧客名を読み取る方法がわからないことです。

予約システムを作っていますが、顧客がすでに存在することを知る必要があります。そのため、誰かが既に顧客であるかどうかを確認できるように、Customers.txt ファイルを読み取る必要があります。彼がそうでない場合は、ファイルライターで新しいものを作成します(私はすでにコードを持っています)。

この予約制の意味は、理容室に予約を入れることです。予約を Reservations.txt という別の txt ファイルに入れる必要があります。そのファイルでは、各予約時間と予約者を確認できます。

助けてくれてありがとう!

これは私がすでに持っているコードです: (いくつかのコメントはオランダ語ですが、翻訳します)

package nielbutaye;
import java.io.*;
import java.util.UUID;
/**
 * @author Niel
 *
 */
public class Klant {
    //declaration that represents the text
    public static String S;

    public static String NEWLINE = System.getProperty("line.separator");

/**
 * constructor
 */
public Klant(){}

/**
 * @return
 * By giving the name of the customer you will get all the data from  the customer
 */
public double getCustomer() {


    return 0 ;
}

/**
 * Make a new customer
 */

public void setNew customer(){
    // 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 + NEWLINE);
    //Closing the writer
    out.close();


}catch (Exception e){//Catch when there are errors
    System.err.println("Error: " + e.getMessage());
    }
    }
}
4

1 に答える 1

0

ABufferedReader()には という名前のメソッドがreadLine()あり、これを使用してファイルから行を読み取ることができます。

BufferedReader br = new BufferedReader(new FileReader("Customers.txt"));
String line;

while ((line = br.readLine()) != null)
{
    ...
}

br.close();

あなたのWriteToFile()方法から、顧客の詳細は 4 行を占め、最初の行に顧客の名前が表示されているようです。顧客を検索するときは、while読み取った 4 行ごとにのみ調べるようにループを調整します。

その他のポイント:

  • Sをメンバー変数にする理由はないようです。気にしないでstaticください。でローカルStringインスタンスを宣言setNewCustomer()し、引数として に渡すだけWriteToFile()です。
  • NEWLINE変数を定義する代わりに、BufferedWriternewLine()メソッドを使用できます。
于 2012-03-06T17:52:06.797 に答える