首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >java,在keyboardscanner不接收输入时创建循环

java,在keyboardscanner不接收输入时创建循环
EN

Stack Overflow用户
提问于 2018-12-13 02:25:31
回答 2查看 43关注 0票数 0

我试图在keyboardscanner.nextline()没有被接收的时候做一个循环,我被困在这里是因为我找不到解决方案,我甚至不知道这是否可能……下面是我在代码中尝试做的事情……

代码语言:javascript
复制
public String messagingread(String username) throws RemoteException {
    Scanner keyboardScanner = new Scanner(System.in);
        try {
            while (keyboardScanner.nextLine().isEmpty) {
                System.out.println("cant get in here");
      //i can only get in here if the scann is only an enter(isempty), but i //want to get in here if i dont scan anything...i dont want isempty i want not //defined and i dont know how to do it ....
                }
            System.out.println("pls help")
            }       
        }  
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-13 02:34:18

这将在其他线程中执行任务,并接受输入,直到输入为empty。另外,要注意关闭Scanner

代码语言:javascript
复制
class Task implements Runnable {

    private boolean shouldRun = true;

    public void stop() {
        this.shouldRun = false;
    }

    @Override
    public void run() {
        while (this.shouldRun) {
            try {
                Thread.sleep(1000);
                System.out.println("Doing some work every 1 second ...");
            } catch (InterruptedException e) {
                 e.printStackTrace();
            }
        }
        System.out.println("Task have been stopped, Bye!");
        Thread.currentThread().interrupt();
    }
}

 public final class Example {

     public static void main(String[] args) {
         Scanner keyboardScanner = new Scanner(System.in);
         try {
             Task task = new Task();
             // run the task on new Thread
             Thread newThread = new Thread(task);
             newThread.start();
         /*
          read lines while it is not empty:
          (line = keyboardScanner.nextLine()) -> assign the input to line
          !(line ...).isEmpty() -> checks that line is not empty
           */
            System.out.println("Give me inputs");
            String line;
            while (!(line = keyboardScanner.nextLine()).isEmpty()) {
                System.out.println("new line read :" + line);
            }
            // when you give an empty line the while will stop then we stop
            // the task
            task.stop();
        } finally {
            // after the piece of code inside the try statement have finished
            keyboardScanner.close();
        }
        System.out.println("Empty line read. Bye!");
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-12-13 02:35:02

代码语言:javascript
复制
// retrieve not empty line
public static String messagingread(String username) {
    try (Scanner scan = new Scanner(System.in)) {
        while (true) {
            String line = scan.nextLine();

            // do it while line is empty
            if (!line.isEmpty())
                return line;
        }
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53749172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档