[英]How to read ArrayList from text file but every 3 lines
Here is the sample text file: 这是示例文本文件:
Brisbane 布里斯班
03163012 03163012
Australia 澳大利亚
Tokyo 东京
041022200 041022200
Japan 日本
now I want to read three data together then put in different variables. 现在我想一起读取三个数据,然后放入不同的变量。 Then take another three and so on.
然后再取三个,依此类推。
location = brisbane;
phoneNumber = 03163012;
country = Australia;
Afterwards, passed into the constructor. 之后,传递给构造函数。
And there is a MAXIMUM LOCATION = 10 that have to be read 并且必须读取一个最大位置= 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();
}
Can anyone help me with this one. 谁能帮我这个忙。 I dont know what should I write in ????
我不知道该怎么写? thanks in advance
提前致谢
You probably want something like this inside your for loop: 您可能希望在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++;
You want to read 3 lines, and add to locations, then phoneNumber, then country. 您想读取3行,然后依次添加到位置,电话号码和国家/地区。 There's all kinds of other problems with your code though (like a misplaced
}
and newFileReader
) 不过,您的代码还有其他各种问题(例如放错位置的
}
和newFileReader
)
Use Guava's method that reads file by lines and gives output as List<String>
使用Guava的方法来逐行读取文件,并以
List<String>
输出
Files.readLines(java.io.File, java.nio.charset.Charset) Files.readLines(java.io.File,java.nio.charset.Charset)
Using it will make your code look much simplier, you will get rid of all those try-catch-finally
and file buffers. 使用它会使您的代码看起来更简单,您将摆脱所有那些
try-catch-finally
和文件缓冲区。
Iterate that list and use those strings as variables for: 迭代该列表,并将这些字符串用作以下变量:
location = brisbane;
phoneNumber = 03163012;
country = Australia;
iterating can look like this: 迭代看起来像这样:
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);
}
}
Note that in code above I filled my list, yours fill be filled after reading file. 请注意,在上面的代码中,我填写了我的列表,读取文件后将填写您的列表。
What you need is another object for location, so you can store your values like this: 您需要的是另一个用于定位的对象,因此您可以像这样存储值:
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
}
The MyLocation class will look like this: 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.