Mooc 赫尔辛基第 1 部分 第 4 周 练习 18 Java Weird Occurrence

Mooc Helsinki Part 1 Week 4 Exercise 18 Java Weird Occurrence

提问人:Haython 提问时间:6/28/2022 更新时间:7/6/2022 访问量:349

问:

这里有这个错误的问题,解决方案简直超出了我的范围。

失败: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());
        }

    }
}

java 数组 for-loop oop java.util.scanner

评论

3赞 user16320675 6/28/2022
真正的错误始于 - 可能的原因:调用时没有更多内容可以从标准输入中读取 - 我看不到输入,但也许你应该用它来测试是否还有可以读取的输入(顺便说一句,该错误消息的开头不是标准的 Java,更像是有人在搞笑)java.util.NoSuchElementExceptionscanner.next()scanner.hasNext()
1赞 tgdavies 6/28/2022
也许他们正在发送 EOF 而不是空字符串的换行符。在本地尝试一下。(即在 linux 上键入 CTRL-D 或在 Windows 上键入 CTRL-Z)
1赞 Basil Bourque 6/28/2022
可怕的标题。重写以总结您的具体技术问题。

答:

0赞 Haython 6/29/2022 #1

使用 scanner.nextLine() 而不是 scanner.next() 求解。不知道在这种情况下如何产生如此大的差异

0赞 xthe_white_lionx 7/6/2022 #2
  1. 尽可能对变量使用更通用的类型,因为如果您使用 Arraylist 的 LinkedList,并且它具有与您正在使用的方法相同的方法,它不会更改逻辑。
  2. 对于字符串,请使用 scanner,nextLine()
  3. numberId 或简称 id 应该是一个数字整数会很棒,所以使用 scanner.nextInt() 在这里只获取整数
  4. 在类 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();
    }
}