我对这种方法有困难。该方法应该读取由逗号分隔的一系列数据(攻击ID int、以MM/DD/YYYY格式保存的日期字符串、怪物字符串的名称、位置字符串和攻击字符串的报告)的文本文件,并将这些值放入名为monsterAttacks的monsterAttacks中。每次运行此方法时,我都会得到一个InputMismatchException。我有一种感觉,它与日期有关,但在本例中,我不确定在何处或如何使用String ()方法。我怎样才能使它正常工作呢?
免责声明:这是作业作业的一部分。
提前谢谢。
编辑:文本文件中的示例数据:
1994年12月23日,加利福尼亚州德古拉,Trisha Takinawa
1992年12月25日至11日,纽约哥斯拉,David
private void readFromFile(){
if(!(monsterAttacks.isEmpty())) {
monsterAttacks.clear();
System.out.println("\nList cleared...");
}
System.out.println("Enter path: ");
String pathName = getUserInput();
File file = new File(pathName);
Scanner read;
MonsterAttack attack;
try {
read = new Scanner(file);
do {
int id = read.nextInt();
String date = read.next();
String name = read.next();
String location = read.next();
String reporter = read.next();
attack = new MonsterAttack(id, date, name, location, reporter);
monsterAttacks.add(attack);
} while (read.hasNext());
read.close();
} catch(IOException e){
e.printStackTrace();
}
}发布于 2017-10-02 02:40:39
你告诉我们你的数据是
用逗号分隔
如果是这样的话,那么您必须考虑到这些令牌分隔符。在这里进行操作的一种方法是只在一行中阅读,然后用逗号分隔以访问每个术语:
try {
read = new Scanner(file);
do {
String line = read.nextLine();
String[] parts = line.split(",\\s*");
int id = Integer.parseInt(parts[0]);
String date = parts[1];
String name = parts[2];
String location = parts[3];
String reporter = parts[4];
attack = new MonsterAttack(id, date, name, location, reporter);
monsterAttacks.add(attack);
} while (read.hasNext());
read.close();
} catch(IOException e){
e.printStackTrace();
}发布于 2017-10-02 03:02:37
我强烈建议只使用文件阅读器,它有您需要的一切,Java 8附带的streams集合提供了一些您可以对给定输入执行的很好的操作。
以下是代码:
final File definitions = Paths.get("some/dir", "monster_definitions.txt").toFile();
final BufferedReader reader = new BufferedReader(new FileReader(definitions));
final String[] entries = reader.lines().collect(Collectors.joining()).split(")");
for(String entry : entries){
final String[] data = entry.substring(1, entry.lastIndexOf(entry)-1).split(",");
final int id = Integer.parseInt(data[0]);
final String date = data[1];
final String name = data[2];
final String location = data[3];
final String reporter = data[4];
monsterAttacks.add(new MonsterAttack(id, date, name, location, reporter));
}
reader.close();现在,我们首先得到所有行的流,然后将每一行收集成一个最后的字符串。我们用")“分隔的字符串,因为这是每个条目的结束标记。然后循环遍历每个条目,并返回条目的一个子字符串。从索引1开始,以最后一个指数减去1结束,我们这样做只是为了去掉"(“和")”。现在,我们得到了包含缓存定义所需的所有信息的原始条目。我们使用",“作为正则表达式来拆分条目,从而得到每个单独数据条目的数组。
但是,我确实鼓励您在这种定义、序列化和反序列化中使用JSON。使用它要容易得多,并且在处理数据时提供了更多的灵活性。
编辑:只是注意到每个条目都没有任何分隔符。除非每个条目都被断线分开了。在这种情况下,你可以做这样的事情:
final List<String> entries = new ArrayList<>(reader.lines().collect(Collectors.toList()));
for(String entry : entries){`发布于 2017-10-02 12:46:20
考虑到您的项目使用Java8,您可以使用流(使用line())简单地操作文件数据,并将它们映射到所需的类(使用map()),在本例中是MonsterAttack。会是这样的:
public void readFromFile(String path) throws Exception {
final File definitions = Paths.get(path).toFile();
final BufferedReader reader = new BufferedReader(new FileReader(definitions));
monsterAttacks = reader.lines().map(line -> {
String[] entry = line.split(",");
return new MonsterAttack(Integer.parseInt(entry[0]), entry[1], entry[2], entry[3], entry[4]);
}).collect(Collectors.toList());
reader.close();
}希望能帮上忙。
https://stackoverflow.com/questions/46518791
复制相似问题