java,在键盘扫描程序不接收输入时创建循环
我试图在未收到keyboardscanner.nextline()时进行循环。我被困在这里,因为我找不到解决方案,我甚至不知道这是否可能。。。下面是我在代码中尝试做的事情java,在键盘扫描程序不接收输入时创建循环,java,Java,我试图在未收到keyboardscanner.nextline()时进行循环。我被困在这里,因为我找不到解决方案,我甚至不知道这是否可能。。。下面是我在代码中尝试做的事情 public String messagingread(String username) throws RemoteException { Scanner keyboardScanner = new Scanner(System.in); try { while (keyboar
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")
}
}
这将在其他线程中执行任务并接受输入,直到输入为空。另外,请注意关闭
扫描仪
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!");
}
}
这将在其他线程中执行任务并接受输入,直到输入为空。另外,请注意关闭
扫描仪
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!");
}
}
谢谢你的回答,但我仍然有同样的问题,当我做键盘扫描时。nextLine()我在等待输入,但我不想等待我想做一个循环,直到我做一个input@RuiAntunes我已经更新了我的答案。现在我包括了一个新的
线程
,它将在main接受输入时工作。当收到空输入时,所有工作都将停止。谢谢你的回答,但当我使用键盘扫描仪时,我仍然有相同的问题。nextLine()我正在等待输入,但我不想等待,我想循环,直到我完成输入input@RuiAntunes我已经更新了我的答案。现在我包括了一个新的线程
,它将在main接受输入时工作。当收到一个空输入时,所有工作都将停止。谢谢你的回答,但当我使用键盘扫描仪时,我仍然有相同的问题。nextLine()我正在等待输入,但我不想等待我想循环直到我输入谢谢你的回答,但我仍然有相同的问题,当我做keyboardScanner.nextLine()时,我在等待输入,但我不想等待,我想在输入之前循环