提问人:Haython 提问时间:6/28/2022 更新时间:7/6/2022 访问量:349
Mooc 赫尔辛基第 1 部分 第 4 周 练习 18 Java Weird Occurrence
Mooc Helsinki Part 1 Week 4 Exercise 18 Java Weird Occurrence
问:
这里有这个错误的问题,解决方案简直超出了我的范围。
失败:PersonalInformationCollectionTest testInputFirst 奇怪的事情发生了。可能是类 PersonalInformationCollection 的 void main (String[] args) 方法消失了,或者您的程序由于异常而崩溃。更多信息:java.util.NoSuchElementException。
然后它说同样的话,但对 testInputSecond 也是如此。
找不到任何错误的原因。在网上查看了正确的解决方案,也许这只是我的视力不佳,但我看不出我的错误代码和他们的正确代码之间有什么区别。
提前感谢您的任何帮助。
import java.util.ArrayList;
import java.util.Scanner;
public class PersonalInformationCollection {
public static void main(String[] args) {
// implement here your program that uses the PersonalInformation class
ArrayList<PersonalInformation> infoCollection = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("First name: ");
String firstName = scanner.next();
if (firstName.isEmpty()){
break;
}
System.out.println("Last name: ");
String lastName = scanner.next();
System.out.println("Identification number: ");
String idNumber = scanner.next();
infoCollection.add(new PersonalInformation(firstName, lastName, idNumber));
}
for (PersonalInformation personalInfo : infoCollection){
System.out.println(personalInfo.getFirstName() + " " + personalInfo.getLastName());
}
}
}
答:
0赞
Haython
6/29/2022
#1
使用 scanner.nextLine() 而不是 scanner.next() 求解。不知道在这种情况下如何产生如此大的差异
0赞
xthe_white_lionx
7/6/2022
#2
- 尽可能对变量使用更通用的类型,因为如果您使用 Arraylist 的 LinkedList,并且它具有与您正在使用的方法相同的方法,它不会更改逻辑。
- 对于字符串,请使用 scanner,nextLine()
- numberId 或简称 id 应该是一个数字整数会很棒,所以使用 scanner.nextInt() 在这里只获取整数
- 在类 PersonalInformation 中实现/重写 toString 方法,以在某一点重新设置字符串
类 PersonalInformation
public class PersonalInformation {
private final int id;
private final String firstName;
private final String lastName;
public PersonalInformation(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
private int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public String toString() {
return "PersonalInformation{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
PersonalInformationCollection 类
public static class PersonalInformationCollection {
public static void main(String[] args) {
// implement here your program that uses the PersonalInformation class
List<PersonalInformation> infoCollection = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Identification number: ");
int id = scanner.nextInt();
if (id < 0){
break;
}
System.out.print("First name: ");
String firstName = scanner.nextLine();
System.out.print("Last name: ");
String lastName = scanner.next();
infoCollection.add(new PersonalInformation(id, firstName, lastName));
}
for (PersonalInformation personalInfo : infoCollection){
System.out.println(personalInfo);
}
scanner.close();
}
}
评论
java.util.NoSuchElementException
scanner.next()
scanner.hasNext()