我不知道如何阅读/处理文件。
示例文件:
2
cow
moo
black
and white very fast
pig
pink
very slow
2表示文件中的条目数。空行。然后是对象的名称和两行特征。
基本上我很困惑是逐行读取输入并处理输入的最佳方法,因此我可以创建指定的对象。
我在想我可以创建一个arraylist然后从那里去,但我不知道如何删除空行分隔符。由于每个文件的元素数量可能不同,我不确定如何将其考虑在内。
答案 0 :(得分:0)
听起来你的问题更多的是概念层面而不是语法层面,所以我会尝试在那个层面上回答。
首先,我不认为一开始就需要数字,但你可以用它来检查你事后的进展情况。
我认为你应该创建一个描述你的对象的类,让我们称它为" Animal"课程供参考。构造函数应该采用一个数组列表,该列表仅包含与一个特定动物(您正在实例化的动物)相关的行的子集。该类的属性应该是您需要的(例如名称等)。
所以在伪代码中:
准备阵列列表:
-read each line of file into arraylist
消耗arraylist:
-read and save off the number of entries for checking later
-read and ignore the first blank line
-read line by line into another arraylist until the next blank line, pass your new array list to the constructor for your Animal class and then repeat this until you hit the end of file (making sure to construct the final object as well).
"动物"构造:
-Set the first entry in the arrayList passed to constructor as the animals Name.
-Set remaining properties as required....
当您创建动物时,您也可以将它们存储在某个数组中,最后您可以验证是否使用您在开始时存储的值创建了正确的数字。