检测java中的回车键 [英] Detecting enter key in java
本文介绍了检测java中的回车键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用扫描仪获取用户输入,如果用户按Enter键,则继续执行下一个输入语句。但它一次打印。
I have tried to get user input with scanner and if user press enter then proceed to next input statement. but it print all at once.
public class MainRDS
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String path;
String name;
String ext;
String date;
System.out.println("Directory search by path, name, extension, content and date.");
System.out.print("\nEnter Starting directory for the search (link c:"+"\\"+"temp) : ");
path = in.next();
System.out.print("\nEnter the file name (like myFile or enter for all) : ");
if((name = in.nextLine()).length() > 0)
System.out.print("\nEnter the file extenstion (like txt or enter for all) : ");
if((ext = in.nextLine()).length() > 0)
System.out.print("\nEnter last modified date (like 11/21/2012 or enter for any) : ");
date = in.nextLine();
}
}
输出:
Directory search by path, name, extension, content and date.
Enter Starting directory for the search (link c:\temp) : c:
Enter the file name (like myFile or enter for all) : myfile
Enter last modified date (like 11/21/2012 or enter for any) :
必需输出:
Directory search by path, name, extension, content and date.
Enter starting directory for the search (like c:\temp): c:\temp
Enter the file name (like myFile or enter for all):
Enter the file extension (like txt or enter for all): txt
Enter content to search for (like comp121 or enter for any):
Enter last modified date (like 11/21/2013 or enter for any): 11/1/2011
推荐答案
知道了!
您需要使用:
path = in.nextLine();
否则, name = in.nextLine()
捕获路径的输入键并且没有任何值。
Otherwise, name = in.nextLine()
catches the path's "enter" key and doesn't get any value.
这篇关于检测java中的回车键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文