0

私は次のようなユーザー入力を求めるプログラムを作成しました。

System.out.println("Where would you like the output file to end up? (full path and desired file name): ");
Scanner out_loc = new Scanner(System.in);
output_loc = out_loc.nextLine();

..。

System.out.println("Hey, please write the full path of input file number " + i + "! ");
System.out.println("For example: /home/Stephanie/filein.txt");
Scanner fIn = new Scanner(System.in);

この方法で何度か入力を求めますが、入力を間違えると、プログラムを強制終了して再実行する必要があるため、非常に苦痛になる可能性があります。プログラムを実行するときに一度にすべての入力を取得する簡単な方法はありますか?実行するときにコマンドラインで宣言するのと同じように?

java -jar /home/Stephanie/NetBeansProjects/cBelow/dist/cBelow.jar -userinputhere?
4

2 に答える 2

2

ファイルリダイレクトを使用できます。

program < file

プログラムの標準入力にファイルを送信します。あなたの場合、

java -jar /home/Stephanie/NetBeansProjects/cBelow/dist/cBelow.jar-userinputhere<ファイル

または、プログラム内のファイルから読み取ることができます。あなたはこれを次のようにオプションにすることができます

InputStream in = args.length < 1 ? System.in : new FileInputStream(args[0]);
Scanner scan = new Scanner(in); // create the scanner just once!
于 2012-11-05T16:54:54.070 に答える
0

次のようにコマンドを実行すると:

java -jar /home/Stephanie/NetBeansProjects/cBelow/dist/cBelow.jar -userinputhere?

プライマリクラスのメソッドを実行し、次のように直接public static void main(String[] args)取得できます。userinputhere

  public static void main(String[] args)
     String userinputhere = args[0];
     ..... rest of your code
   }

複数のユーザー入力がある場合は、それらすべてを次のように取得できます。

  public static void main(String[] args)
      String userinput1 = args[0];
      String userinput2 = args[1];
      String userinput3 = args[2];
      //and so on..
      ..... rest of your code
  }
于 2012-11-05T16:56:46.587 に答える