如何捕获回车键,使用扫描仪作为控制台输入? [英] How to capture enter key, using scanner as console input?
问题描述
我想捕捉在控制台中输入字符。我输入2串。
I want to capture enter character in console. I am inputting 2 strings.
案例1. removestudent(pressing进入)移除所有从数组列表中的学生。
Case 1. removestudent(pressing enter) removes all the students from array list.
案例2. removestudent student1从数组列表中删除students1。
Case 2. removestudent student1 removes students1 from array list.
Scanner in=new Scanner();
type_op=in.next();
param=in.next();
if (type_op.equals("removestudent"))
{
//Calling remove student function and passing param over here.
}
现在的情况下2工作正常。但是,对于案例1,我想,当用户presses输入参数值为空。然后,我将通过参数为空值,以我的删除功能,并删除阵列列表中的所有学生。
Now the case 2 works fine. However, for Case 1, I want param value to be null when user presses enter. I will then pass param as a null value to my remove function and delete all the students in the array list.
list1.clear();
请帮助我知道如何得到这个回车键。
Please help me in knowing how to get this enter key.
推荐答案
您可以阅读线,如果线是空的,你可以假设它是回车键..喜欢低于code ..
You can read line and if line is blank, You can assume it is enter key.. like below code..
Scanner scanner = new Scanner(System.in);
String readString = scanner.nextLine();
System.out.println(readString);
if (readString.equals(""))
System.out.println("Enter Key pressed.");
if (scanner.hasNextLine())
readString = scanner.nextLine();
else
readString = null;
这篇关于如何捕获回车键,使用扫描仪作为控制台输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!