如何解析文本文件/需要在 switch 语句中使用第一个字符
我需要使用分隔符“:”解析文本文件
文本文件如下所示:
a : 29173 : He : James : 474937 : 2 : 4 : 1
t : 27184 : She : Susan: 474930 : 6 : 4 : 2
c : 28174 : 他 : 梅格 : 474931 : 5 : 4 : 1
p : 29190 : She : Robin : 474947 : 4 : 4 : 4
我的第一个问题是如何解析 switch 语句中使用的第一个字符? 我在想类似下面的代码(我把它们放在一起......但我很确定它是不正确的)。
我知道我可以读取第一个字符 &我知道我可以使用带有字符的 switch 语句,但我不知道如何将两者放在一起。
File file = new File("test.txt");
// Read the file
try {
Scanner scanner = new Scanner(file).useDelimiter(":");
String line = scanner.nextLine();
while (scanner.hasNextLine())
{
line = line.trim();
int index;
String type;
String name;
String identifier = scanner.next();
switch(line.charAt(0)) {
case 'p':
index = scanner.nextInt();
name = scanner.next();
break;
case 'c':
index = scanner.nextInt();
type = scanner.next();
name = scanner.next();
int partyC = scanner.nextInt();
int empathyC = scanner.nextInt();
int carryingCapacityC = scanner.nextInt();
break;
case 't':
index = scanner.nextInt();
type = scanner.next();
int creatureT = scanner.nextInt();
int weightT = scanner.nextInt();
int valueT = scanner.nextInt();
break;
case 'a':
index = scanner.nextInt();
type = scanner.next();
int creatureA = scanner.nextInt();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
VS。
好吧,我已经阅读并尝试了一些,并且认为我可以使用 if-else 循环。但是,我遇到了许多错误(在代码下方)。
File file = new File("test.txt");
// Read the file
try {
Scanner scanner = new Scanner(file).useDelimiter(":");
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
line = line.trim();
int index;
String type;
String name;
char identifier = line.charAt(0);
if (identifier == 'p') {
index = scanner.nextInt();
name = scanner.next();
} else if (identifier == 'c') {
index = scanner.nextInt();
type = scanner.next();
name = scanner.next();
int partyC = scanner.nextInt();
int empathyC = scanner.nextInt();
int carryingCapacityC = scanner.nextInt();
} else if (identifier == 't') {
index = scanner.nextInt();
type = scanner.next();
int creatureT = scanner.nextInt();
int weightT = scanner.nextInt();
int valueT = scanner.nextInt();
} else if (identifier == 'a') {
index = scanner.nextInt();
type = scanner.next();
int creatureA = scanner.nextInt();
} else {
System.out.println("This is not a valid line of input");
} System.out.println("Identifier: " + identifier);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
错误:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at SorcerersCave.main(SorcerersCave.java:33)
I need to parse a text file using the delimiter ":"
Text file looks like this:
a : 29173 : He : James : 474937 : 2 : 4 : 1
t : 27184 : She : Susan: 474930 : 6 : 4 : 2
c : 28174 : He : Meg : 474931 : 5 : 4 : 1
p : 29190 : She : Robin : 474947 : 4 : 4 : 4
My first question is how can/can I parse the first character to use in a switch statement?
I was thinking something like the code below (which I put together... but am pretty sure is incorrect).
I know that I can read the first char & I know that I can use a switch statement with char(s) but I'm stuck on how to put the two together.
File file = new File("test.txt");
// Read the file
try {
Scanner scanner = new Scanner(file).useDelimiter(":");
String line = scanner.nextLine();
while (scanner.hasNextLine())
{
line = line.trim();
int index;
String type;
String name;
String identifier = scanner.next();
switch(line.charAt(0)) {
case 'p':
index = scanner.nextInt();
name = scanner.next();
break;
case 'c':
index = scanner.nextInt();
type = scanner.next();
name = scanner.next();
int partyC = scanner.nextInt();
int empathyC = scanner.nextInt();
int carryingCapacityC = scanner.nextInt();
break;
case 't':
index = scanner.nextInt();
type = scanner.next();
int creatureT = scanner.nextInt();
int weightT = scanner.nextInt();
int valueT = scanner.nextInt();
break;
case 'a':
index = scanner.nextInt();
type = scanner.next();
int creatureA = scanner.nextInt();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
VS.
Alright so I've read-up and played around with this some and think I can just use an if-else loop. However, I'm getting a number of errors (below the code).
File file = new File("test.txt");
// Read the file
try {
Scanner scanner = new Scanner(file).useDelimiter(":");
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
line = line.trim();
int index;
String type;
String name;
char identifier = line.charAt(0);
if (identifier == 'p') {
index = scanner.nextInt();
name = scanner.next();
} else if (identifier == 'c') {
index = scanner.nextInt();
type = scanner.next();
name = scanner.next();
int partyC = scanner.nextInt();
int empathyC = scanner.nextInt();
int carryingCapacityC = scanner.nextInt();
} else if (identifier == 't') {
index = scanner.nextInt();
type = scanner.next();
int creatureT = scanner.nextInt();
int weightT = scanner.nextInt();
int valueT = scanner.nextInt();
} else if (identifier == 'a') {
index = scanner.nextInt();
type = scanner.next();
int creatureA = scanner.nextInt();
} else {
System.out.println("This is not a valid line of input");
} System.out.println("Identifier: " + identifier);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Errors:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at SorcerersCave.main(SorcerersCave.java:33)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你可以逐行读取文件。获取要切换的第一个字符,如下所示:
不要使用分隔符并执行
.next()
,而是使用String[] parts = line.split(":")
这给了你字符串数组。并修剪每个元素,对于整数使用 Integer.parseInt(parts[index].trim()) 。另外,您不需要
while (scanner.hasNext())
。相反做I think you can just read the file line-by-line. Get the first char to switch as follows:
And instead of using a delimiter and doing
.next()
, useString[] parts = line.split(":")
that gives you array of strings. And trim each element and for ints useInteger.parseInt(parts[index].trim())
.Also, you don't need
while (scanner.hasNext())
. Instead do