0

スキャナーでユーザー入力を取得しようとしましたが、ユーザーが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 
4

4 に答える 4

1

とった!

あなたが使用する必要があります:

path = in.nextLine();

それ以外の場合はname = in.nextLine()、パスの「Enter」キーをキャッチし、値を取得しません。

于 2013-03-06T07:38:03.883 に答える
1

あなたのコードは完全に問題ありません。最初の行を置き換えるだけです

Scanner in = Scanner(System.in);

Scanner in =new Scanner(System.in);


Scanner in = Scanner(System.in);これはこの行のために実行中のコードではないため、このコードをどのようにテストしたか理解できません

于 2013-03-06T07:24:36.417 に答える
0

変化する

path = in.next();

path = in.nextLine();

それをテストし、すべてが印刷され、正常に動作しました!

于 2013-03-06T07:26:30.303 に答える
0

あなたのコードをテストしたところ、ユーザーが「Enter」キーを押すようになりました。探しているものをより具体的に説明できますか? 他の人が述べているように、スキャナーを変更するだけです:

Scanner in = new Scanner(System.in);
于 2013-03-06T07:22:00.473 に答える