0

这是示例文本文件:

布里斯班
03163012
澳大利亚
东京
041022200
日本

现在我想一起读取三个数据然后放入不同的变量。然后再拿三个,依此类推。

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 行,然后添加到位置,然后是电话号码,然后是国家/地区。不过,您的代码还存在各种其他问题(例如错位的}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 回答