【问题标题】:acccepting file names using command line in java在java中使用命令行接受文件名
【发布时间】:2014-04-26 00:55:41
【问题描述】:

我正在尝试通过命令行接受 3 个文件名。这是我尝试过但不起作用的代码.. ??请帮忙

public class MedicalStudentMatcher {

enum Arguments {
    HospitalFile, ResidentFile, OutputFile
};

/**
 * @param args
 */
public static void main(String[] args) {

    //Retrieve file locations from command line arguments
    String hospitalFile = "";
    String residentFile = "";
    String outFile = "";


    if (args.length > 2){
        hospitalFile = args[Arguments.HospitalFile.ordinal()];
        residentFile = args[Arguments.ResidentFile.ordinal()];
        outFile = args[Arguments.OutputFile.ordinal()];
    } else {
        System.out
                .println("Please include names for the preference files and output file when running the application.\n "
                        + "Usage: \n\tjava MedicalStudentMatcher hospital.csv student.csv out.txt\n");
        return;
    }

【问题讨论】:

  • 你得到的确切错误是什么?
  • 你是在命令行中传递名字吗?
  • 好吧,它进入 else 循环.. 它不接受值
  • 你的意思是else branch,无论如何,args[Arguments.HospitalFile.ordinal()] 被过度设计了。如果您不能使用纯整数索引,则最好使用命令行参数解析器。看这里stackoverflow.com/questions/1524661/…
  • 认真考虑使用类似 commons-cli commons.apache.org/cli

标签: java enums


【解决方案1】:

做一些调试。打印命令行参数以及每个参数的长度

类似:

System.out.println(args.length);

for(String arg: args)
{
   System.out.println(arg);
}

这样您将看到您作为参数传递给程序的内容。

【讨论】:

  • 谢谢.. 试过了,参数的长度是 0... 不知道为什么.. 这就是我卡在的地方
  • 你是如何运行你的应用程序的。是从命令提示符/控制台运行它吗?
  • @user3438489 右键点击哪里?您是在 Eclipse 还是其他 IDE 中?
  • 您使用的是哪个 IDE?
  • 在 Eclipse 中,右键单击您的项目,选择 "Run As"-> "Run Configurations" 。选择“(x)=Arguments”选项卡并在 Program arguments 下输入文件列表
猜你喜欢
  • 2014-05-07
  • 2013-01-08
  • 2011-02-02
  • 1970-01-01
  • 2017-05-08
  • 2018-05-01
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多