有没有一种方法可以使用scanner().nextLine方法用一个数字来标记一个特定的名称——如果它们有相同的名称,那



我想找到一种方法,如果我扫描以下文本文件,

Tom Cat,mouse.pizza,yes,10 - pizza,vegetable
Don Dog,zoe.justice,yes,10 - pizza,vegetable
Michael Pog,james.george,yes,10 - pizza,vegetable
Judy Elsa,lawrence.holden,no,10 - pizza,vegetable
Ooga Balooga,james.george,yes,10 - pizza,vegetable
James Harnold Pop,animal.lover,yes,10 - pizza,vegetable
Pizza Lover One,james.george,yes,10 - pizza,vegetable
Animal Instincts Two,james.george,yes,10 - pizza,vegetable
Jacob House,james.george,no,10 - Analyst,pizza,vegetable
Larry Lobster,harold.father,yes,10 - pizza,vegetable
Sponge Eater,donald.boo,no,10 - pizza,vegetable
Tristan Baloonga,james.george,yes,10 - pizza,vegetable

我可以将mouse.pizza标记为1,将zoe.justice标记为2,将james.george标记为3,将animal.lover标记为4,将jame.george再次标记为3。

我现在有这个:

Scanner scanner = new Scanner(file);
int id2 = 0;
int rid = id2;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String name = line;
String[] parts = name.split(",");
String fullname = parts[1];
String name2;
name2 = scanner.next();
String[] parts2 = name2.split(",");
String fullname2 = parts2[1];

if(fullname == fullname2){
recruiterid = id2;
}

else {
rid = id2++;
}

而且我似乎无法正确编译代码。感谢您的帮助。谢谢

试试这个。

Map<String, Integer> labels = new HashMap<>();
int[] n = {0};
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
int label = labels.computeIfAbsent(line.split(",")[1], k -> ++n[0]);
System.out.println(label + " : " + line);
}
}

输出

1 : Tom Cat,mouse.pizza,yes,10 - pizza,vegetable
2 : Don Dog,zoe.justice,yes,10 - pizza,vegetable
3 : Michael Pog,james.george,yes,10 - pizza,vegetable
4 : Judy Elsa,lawrence.holden,no,10 - pizza,vegetable
3 : Ooga Balooga,james.george,yes,10 - pizza,vegetable
5 : James Harnold Pop,animal.lover,yes,10 - pizza,vegetable
3 : Pizza Lover One,james.george,yes,10 - pizza,vegetable
3 : Animal Instincts Two,james.george,yes,10 - pizza,vegetable
3 : Jacob House,james.george,no,10 - Analyst,pizza,vegetable
6 : Larry Lobster,harold.father,yes,10 - pizza,vegetable
7 : Sponge Eater,donald.boo,no,10 - pizza,vegetable
3 : Tristan Baloonga,james.george,yes,10 - pizza,vegetable

最新更新