[英]Java Input Output Logic
首先,我要感谢这四个人,因为我发现自己在此论坛上的所有材料以及不同成员一直在给我的所有帮助中迅速进步。 所以,这一切都非常感谢。 至于我的问题,我一直在尝试输入输出,并想看看这种逻辑是否可行。 我试图在适当的数组中获取适当的内容,并希望了解这种逻辑是否可以做到。 当前(一段时间内),我不会在一个可以有效访问任何Virtual IDE的地方,因此所有这些操作都可以使用记事本,单词等即时完成。 * 因此,请不要过分用语法。 我最担心的是逻辑(如果可行的话),以及一个较小的错误,即代码中的任何主要错误。 *
非常感谢。
因此,基本上,文本文件是这样的。 标题,一行空格,然后是姓名,年龄和工资,分隔符是#。 然后在其正下方,分隔符的名称,年龄和工资会带来#等。
(假设Bobby,Sandy,Roger,Eric和David之间没有行间距。因此,在txt文件中,它们之间是彼此相邻的,但是信息和Bobby之间是有差距的。
信息
鲍比#24#5.75
桑迪#19#10.22
罗杰#27#6.73
埃里克#31#8.99
大卫#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();
}
}
这种逻辑是否可以将标题存储在标题字符串,空白数组下的空白行,名称数组下的名称,年龄数组下的年龄和工资数组下的工资?
非常感谢。
PS:最关心的是最后一个while循环,我想知道是否将名称放入名称数组,将年龄放入年龄数组,并将工资放入工资数组。
首先,您只需要一个while循环。 我不明白为什么会有两个,特别是因为第一个条件是无意义的( 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
}
在代码中,您将用PersonData
对象的List
结构替换数组:
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
由于我们使用的是List而不是数组,因此我们不必担心确切知道文件中有多少记录,并且我们也不会因为失去任何信息而冒着丢失信息的风险。储存它。
这样,我们还可以确定地知道姓名,年龄和工资都与同一记录相关,而在我们只是希望说数组中索引为0的所有记录都与同一个人相关时,我们就可以确定这一点。
另外,如果您向记录中添加其他数据(例如,名称#age#wage#最喜欢的食物),您要做的就是向PersonData
对象添加一个新字段,并在解析方法中添加一行以添加该数据数据到对象。 如果使用数组,则需要添加一个新数组,依此类推。
例如,如果您有一行仅包含名称或缺少工资的行,以此类推,那么创建逻辑也容易得多,这样您实际上就能够以某种有意义的方式保存数据。
如果您想在Java或任何OOP语言方面取得良好的进展,则应始终以面向对象的方式解决问题。
对于眼前的问题,您应该始终考虑使用一个类来存储“个人信息”,而不是使用关联数组。
class PersonInfo {
PersonInfo(String name,int age,float wage) {
this.name = name;
this.age = age;
this.wage = wage;
}
String name;
int age;
float wage;
}
上面的代码大致相同...但是它应该给出一个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]));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.