获取用户的意见

时间:2013-03-06 07:44:40

标签: java

我尝试使用扫描仪获取用户输入,如果用户按Enter键,则继续执行下一个输入语句。但它会立刻打印出来。

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 

2 个答案:

答案 0 :(得分:0)

删除\n并使用println。让我知道它是否无效。

答案 1 :(得分:0)

尝试使用in.nextLine()而不是in.next()。