首先,我要感谢这四个人,因为我发现我自己通过这个论坛上的所有材料和所有不同成员给予我的帮助而得到迅速的改善。所以这一切都是非常感谢你的。至于我的问题,我一直在试验输入输出,并想看看这个逻辑是否有效。我试图在它们的适当数组中得到适当的东西,并想看看这个逻辑是否能做到这一点。目前(和有一段时间)我不会在一个可以有效访问任何虚拟IDE的地方,所以所有这些都是使用记事本、word等临时完成的。*所以不要对我的语法太苛刻。我最关心的是逻辑(如果它能起作用),以及代码中的任何重大错误。
非常感谢。
基本上,文本文件是这样的。标题,一行空格,然后是名称、年龄和工资,分隔符是#。然后就在下面,名字,年龄和工资分离器带来#等。
(假装在txt文件中,鲍比、桑迪、罗杰、埃里克和David..so之间没有行距,但在信息和鲍比之间是有差距的。
信息
Bobby#24#5.75 桑迪#19#10.22 罗杰#27#6.73 Eric#31#8.99 David#12#3.50**
这就是我想出的逻辑。
public class Practice {
public static void main(String[] args) throws Exception {
String Name [] = new String [5];
int Age [] = new int [5] ;
double Wage [] = new double [5];
String Blank [] = new String [5];
FileReader inputfile = new FileReader (new File(info.txt));
BufferedReader InputBuffer = new BufferedReader (inputfile);
String Title = InputBuffer.readline (); // to get the title
int count = 0;
while (InputBuffer.readline() = null) { // this while loop grabs the blank under the title
Blank [count] = count;
}
int i = 0;
while (InputBuffer.readline() !=null) {
String Getter = InputBuffer.readline (); // reads line
String splitup= Getter.split(#); // splits it
Name [i] = splitup[i]; // puts name in this array
Age [i] = splitup([i] + 1); // age in this array
Wage [i] = splitup([i] + 2); // wage in this array
}
InputBuffer.close();
}
}这种逻辑是否适用于将标题存储在标题字符串中、空白行在空白数组下、名称数组下的名称、年龄数组下的年龄以及工资数组中的工资中?
非常感谢。
P.S:最关心的是最后一个while循环,我想知道它是否会把名字放在名字数组中,年龄数组中,工资数组中。
发布于 2012-08-05 05:26:54
首先,你只需要一个时间循环。我不明白为什么有两个,特别是因为第一个条件是无意义的( InputBuffer.readline() = null )。
您的循环应该如下所示:
boolean isTitleParsed = false;
String title;
String line;
while ( (line = inputBuffer.readLine()) != null ) {
if (!isTitleParsed) {
// this is the title
title = line;
isTitleParsed = true;
} else if (line.isEmpty()) {
// this is a blank line; logic for dealing with blank lines here
...
} else {
// this is actual person data
String[] personData = line.split("#");
if (personData != null && personData.length == 3) {
String name = personData[0];
String age = personData[1];
String wage = personData[2];
...
}
...
}
}其次,我认为使用数组是完全错误的做法。正如@AVD在他对OP的评论中提到的,List<T>和POJO可能是一个更好的解决方案--而且更易于扩展。
最后:不,正如您所写的,您的第二个循环不会成功地将名称、年龄和工资保存到数组中。您从不增加i,语法splitup([i] + 1)也是错误的。(你可能是指splitup[i+1]。)
使用数组的
如果您真的要使用数组来保存数据,那么您必须这样做:
String[] names = new String[5];
String[] ages = new String[5];
String[] wages = new String[5];
...
int index = 0;
while ( (line = inputBuffer.readLine()) != null && index < 5) {
if (!isTitleParsed) {
...
} else if (line.isEmpty()) {
...
} else {
// this is actual person data
String[] personData = line.split("#");
if (personData != null && personData.length == 3) {
String name = personData[0];
String age = personData[1];
String wage = personData[2];
names[index] = name;
ages[index] = age;
wages[index] = wage;
index++;
} else {
System.err.println("Line " + line + " is malformed and was not saved.");
}
...
}
}请注意,index在0处实例化,但每次将某些内容保存到数组中时都会增加。这样,names[0]将保存名称,names[1]将保存第二个名称,依此类推。
还请注意,我们将给定记录的名称、年龄和工资保存在相同的索引中。所以我们可以期待names[0]持有"Bobby",ages[0]持有"24",wages[0]持有"5.75“--所有这些都与相同的记录相关。
最后,将while循环中的条件修正为(line = inputBuffer.readLine()) != null && index < 5。这意味着我们将继续遍历文件的行,直到行用完(文件结束),或者索引大于5,这就是我们实例化数组的大小。这就是为什么数组是保存这些数据的糟糕结构的原因之一:您必须确切地知道您的文件中有多少记录,并且您可能最终不会一直填充它们(您分配了太多的空间),或者因为您没有更多的空间来存储它们,所以没有保存一些记录。
使用POJO的
保存数据的一个更好的方法是使用POJO --一个普通的旧Java对象。这类对象基本上是一个“数据持有者”对象。
在你的例子中,它应该是这样的:
public class PersonData {
private String name;
private String wage;
private String age;
public PersonData() {
this(null, null, null);
}
public PersonData(String name, String wage, String age) {
this.name = name;
this.wage = wage;
this.age = age;
}
// ... getters and setters here
}在代码中,可以将数组替换为List结构的PersonData对象:
List<PersonData> records = new ArrayList<PersonData>();在while循环中,将保存到这些对象中,而不是保存到数组中:
// in the else in the while loop:
String[] data = line.split("#");
if (data != null && data.length == 3) {
PersonData record = new PersonData(data[0], data[1], data[2]);
records.add(record);
} else {
// error handling for malformed line
}现在,如果您想获取特定记录的数据,只需从记录列表中提取PersonData对象并对其进行查询:
// assuming the first record we scraped was "Bobby#24#5.75"
PersonData person = records.get(0);
person.getName(); // returns "Bobby"
person.getAge(); // returns 24
person.getWage(); // returns 5.75因为我们使用的是一个列表而不是一个数组,所以我们不必担心文件中有多少条记录,而且我们也不会冒丢失信息的风险,因为我们没有地方可以存储它。
这样,我们也可以肯定地知道,一个名字、年龄和工资都与同一记录相关,而在我们之前,我们只是希望,比如说,数组中索引0的所有记录都与同一个人相关。
另外,如果向记录中添加额外的数据--例如,name#age#wage#favorite食品--您所要做的就是向PersonData对象添加一个新字段,并在解析方法中添加一行以将数据添加到对象中。如果使用数组,则需要添加一个新数组,依此类推。
如果你的一行只有一个名字或者缺了工资等等,那么创建逻辑也就容易多了--这样你就能以某种有意义的方式保存数据了。
发布于 2012-08-05 05:57:44
如果您想在Java或任何OOP语言方面取得良好的进展,您应该始终以面向对象的方式处理问题。
对于当前的问题,您应该始终考虑一个类来存储Person信息,而不是使用关联数组。
class PersonInfo {
PersonInfo(String name,int age,float wage) {
this.name = name;
this.age = age;
this.wage = wage;
}
String name;
int age;
float wage;
}代码与above...but大致相同,它应该给出一个PeopleInfo列表作为输出。
List<PersonInfo> peopleInfo = new ArrayList<String>();
boolean isTitleParsed = false;
while ( (line = inputBuffer.readLine()) != null ) {
if (!isTitleParsed) {
// this is the title
title = line;
isTitleParsed = true;
continue;
} else if (line.isEmpty()) {
// this is a blank line; logic for dealing with blank lines here
} else {
String[] personData = line.split("#");
if (personData != null && personData.length == 3) {
peopleInfo.add(new PersonInfo(personData[0],personData[1],personData[2]));
}
}https://stackoverflow.com/questions/11813970
复制相似问题