Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何连续扫描输入?_Java_Oop_Input_Java.util.scanner - Fatal编程技术网

Java:如何连续扫描输入?

Java:如何连续扫描输入?,java,oop,input,java.util.scanner,Java,Oop,Input,Java.util.scanner,我正在尝试用Java编写一个程序,允许用户修改地址簿。我已经编写了创建AddressBook的代码和允许修改它的函数: public class AddressBookEntry { private String name, phone, email; public AddressBookEntry(String name, String phone, String email) { this.name = name; this.phon

我正在尝试用Java编写一个程序,允许用户修改地址簿。我已经编写了创建AddressBook的代码和允许修改它的函数:

public class AddressBookEntry 
{
    private String name, phone, email;
    public AddressBookEntry(String name, String phone, String email)
    {
        this.name = name;
        this.phone = phone;
        this.email = email;
    }
    public String getName() 
    {
        return name;
    }
    public String getPhone() 
    {
        return phone;
    }
    public String getEmail() 
    {
        return email;
    }
}

我现在正试图编写另一个类,它使用用户输入来执行上述函数。我需要连续扫描输入

我尝试过使用Scanner,但只能在程序提示输入时使用,例如键入要添加的条目:。在没有程序提示输入的情况下解决此问题的最佳方法是什么?

在退出条件下使用此选项:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

while (...) {
    System.out.print("Type an entry to add: ");
    String input = bufferedReader.readLine();
    ...
}
现在在输入中有了用户输入的字符串

例如:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

while (true) {
    System.out.print("Type an entry to add: ");
    String input = bufferedReader.readLine();
    if (input.equals("exit"))
        break;
    ...
}

当用户键入exit时,此程序将退出。

这可能回答了您的问题:我曾尝试使用Scanner,但只有当程序提示输入时才能使其正常工作,例如,如果您操作错误,并且由于您没有包含该代码,我们无法帮助您修复该问题。如果需要,您可以更新您的问题。
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

while (true) {
    System.out.print("Type an entry to add: ");
    String input = bufferedReader.readLine();
    if (input.equals("exit"))
        break;
    ...
}