gpt4 book ai didi

Java StringTokenizer 错误

转载 作者:太空宇宙 更新时间:2023-11-04 13:58:06 25 4
gpt4 key购买 nike

我的一种方法有问题,导致我出现错误,不允许我破坏代码。方法如下。

    public static List<Student> getStudentList(java.io.File irStudentsFile) {
List<Student> students = new LinkedList<>();
Stream<String> lines;
try {
lines = java.nio.file.Files.lines(irStudentsFile.toPath());
} catch (IOException ex) {
return null;
}

lines.forEach((Consumer<? super String>) line ->
{
Student student = new Student();
StringTokenizer sToken = new StringTokenizer(line, " ");
student.setIdStudent(Integer.valueOf(sToken.nextToken()));
student.setName(sToken.nextToken());

students.add(student);
}

);

return students;
}

我收到在线错误。 StringTokenizer sToken = new StringTokenizer(line, "");

错误提示“StringTokenizer 中的 StringTokenizer (java.lang.String String) 无法应用于 (java.lang.Object String)”

我该如何解决这个问题?

谢谢

最佳答案

我相信您的代码中还有另一个错误。查找下面的示例(这是您在工作示例中的代码)

public class StudenDemo {

public static void main(String[] args) {
List<Student> list = getStudentList(new File("students.txt"));
System.out.println("list = " + list);
}

public static List<Student> getStudentList(java.io.File irStudentsFile) {
List<Student> students = new LinkedList<>();
Stream<String> lines;
try {
lines = java.nio.file.Files.lines(irStudentsFile.toPath());
} catch (IOException ex) {
return Collections.EMPTY_LIST;
}

lines.forEach((Consumer<? super String>) line -> {
Student student = new Student();
StringTokenizer sToken = new StringTokenizer(line, " ");
student.setIdStudent(Integer.valueOf(sToken.nextToken()));
student.setName(sToken.nextToken());
students.add(student);
}
);

return students;
}

private static class Student {

private Integer id;
private String name;

public Student() {
}

private void setIdStudent(Integer id) {
this.id = id;
}

private void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Student{" + "id=" + id + ", name=" + name + '}';
}
}
}

文件:students.txt

1 John Doe
2 Jane Doe

输出

list = [Student{id=1, name=John}, Student{id=2, name=Jane}]

关于Java StringTokenizer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29604700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com