0

サンプル テキスト ファイルは次のとおりです。

ブリスベン
03163012
オーストラリア
東京
041022200
日本

ここで、3 つのデータをまとめて読み取り、異なる変数に入れたいと考えています。次に、さらに 3 つ取ります。

location = brisbane;  
phoneNumber  = 03163012;
country = Australia; 

その後、コンストラクタに渡されます。

そして、読まなければならない MAXIMUM LOCATION = 10 があります

public boolean data()
{
boolean isValid = true;
boolean check = true;
int a = 0;

try
{
    BufferedReader reader = new BufferedReader(newFileReader("location.txt"));
    String data = reader.readLine();

    while (data != null && check)
    {
        if (a != MAX_NUMBER)
        {
            for (int i = 0; i < 3; i++)
            {
                ?????????
                locations.add(newLocation);
                a++;
            }
        else
            check = false;

        data =reader.readLine;
        }
}
reader.close();
}

誰でもこれで私を助けることができますか?何を書けばいいのかわからない???? 前もって感謝します

4

3 に答える 3

2

for ループ内で次のようなものが必要になる場合があります。

            locations.add(data);
            data = reader.readLine();
            if(data!=null) 
                phoneNumber.add(data);
            else
                break;
            data = reader.readLine();
            if(data!=null) 
                country.add(data);
            else
                break;
            a++;

3 行を読み取り、location、次に phoneNumber、次に country に追加する必要があります。ただし、コードには他にもあらゆる種類の問題があります( and の配置ミス}などnewFileReader

于 2012-06-17T15:48:44.683 に答える
2

ファイルを行単位で読み取り、次のように出力する Guava の方法を使用します。List<String>

Files.readLines(java.io.File, java.nio.charset.Charset)

try-catch-finallyこれを使用すると、コードがより単純に見えるようになり、それらとファイル バッファーがすべて取り除かれます。


そのリストを繰り返し、それらの文字列を次の変数として使用します。

location = brisbane;  
phoneNumber  = 03163012;
country = Australia;

反復は次のようになります。

public static void main(String[] args) {

            //it's you a mock
    List<String> lines = new ArrayList<String>();
    lines.add("a");
    lines.add("1");
    lines.add("aa");

    lines.add("b");
    lines.add("2");
    lines.add("bb");


            //Iterating String from file.
    for (int i = 0; i < lines.size(); i += 3) {

        String location = lines.get(i);
        String phoneNumber = lines.get(i + 1);
        String country = lines.get(i + 2);

                    //somehow use red variables
        System.out.println(location);
        System.out.println(phoneNumber);
        System.out.println(country);
    }
}

上記のコードでは、リストを埋めましたが、ファイルを読み取った後にあなたのリストが埋められることに注意してください。

于 2012-06-17T15:49:47.467 に答える
0

必要なのは場所の別のオブジェクトであるため、次のように値を保存できます。

String data = reader.readLine();
int counter = 0;
MyLocation newLocation = null;
while (data != null && ((counter/3) != MAX_NUMBER)){
    switch(counter % 3){
        case 0:
            newLocation = new MyLocation();
            newLocation.setLocation(data);
            break;
        case 1:
            newLocation.setPhone(data);
            break;
        case 2:
            newLocation.setCountry(data);
            locations.add(newLocation);
            newLocation = null;
            break;
    }
    counter++;
}

if(null != newLocation){
    //Error management
}

MyLocation クラスは次のようになります。

private String location = null;

private String phone = null;

private String country = null;

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}
于 2012-06-18T12:07:07.520 に答える