用户输入以外的打印提示和扫描仪



我写了一个程序,要求这样的用户输入:

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?

您可以使用文件重定向。

program < file

将文件发送到程序的标准输入。在您的情况下,

java -jar/home/stephanie/netbeansprojects/cbelow/dist/cbelow.jar -userinputhere&lt;文件

或者您可以从程序中的文件中阅读。您可以使此可选

这样
InputStream in = args.length < 1 ? System.in : new FileInputStream(args[0]);
Scanner scan = new Scanner(in); // create the scanner just once!

将命令运行为:

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
  }

最新更新