2017-04-23 140 views
0
public class converter { 

    public static void main(String [] args) { 
    Options opt = new Options(); 

    opt.addOption("I", "in", false, "Eingabeformat (2,8,10,16)"); 
    opt.addOption("O", "out", false, "Ausgabeformat (2,8,10,16)"); 
    opt.addOption("V", "value", true, "Zu konvertierende Zahl"); 

    CommandLineParser parser = new DefaultParser(); 
    String value = "0"; 
    String in = "0"; 
    String out = "0"; 
    int inInt = 0; 
    int outInt = 0; 

    try { 
     CommandLine cl = parser.parse(opt, args); 

     if (cl.hasOption("I")) { 
      in = cl.getOptionValue("I"); 
      System.out.println(in); 
     } else if (cl.hasOption("in")) { 
      in = cl.getOptionValue("in"); 
      inInt = Integer.parseInt(in); 
     } 

     if (cl.hasOption("O")) { 
      out = cl.getOptionValue("O"); 
      outInt = Integer.parseInt(out); 
     } else if (cl.hasOption("out")) { 
      out = cl.getOptionValue("out"); 
      outInt = Integer.parseInt(out); 
     } 

     if (cl.hasOption("V")) { 
      value = cl.getOptionValue("V"); 
     } else if (cl.hasOption("value")) { 
      value = cl.getOptionValue("value"); 
     } 

    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 
} 

你好,對於我的班級,我必須學習如何使用CLI,現在看起來很好。我的問題是:變量'in'在使用cl.getOptionValue(「I」)後總是返回null。有人可以幫忙嗎?CLI:getOptionValue總是返回'null'

回答

2

我假定你所期望的ARGS I,OV有選項值,否則你不會試圖解析。但您只需通過將addOption調用中的第三個參數設置爲true來指定V選項。你應該將它們全部設置爲true如果要指定選項值:

opt.addOption("I", "in", true, "Eingabeformat (2,8,10,16)"); 
    opt.addOption("O", "out", true, "Ausgabeformat (2,8,10,16)"); 
    opt.addOption("V", "value", true, "Zu konvertierende Zahl"); 

順便說一句,你的名字應該以一個大寫「C」類Converter,這是Java約定。

+0

的任務是,ARGS 「價值」 和 「V」 應該是強制性的Args我在,O,出來都是可選的。我的第一個缺點是讓它們變成虛假的,所以用戶不需要輸入它們,如果他不想。但我實際上重做了我的班級,所以這個任務應該完成 - 觀看我的課程。 – Razmo

+0

順便說一句感謝您的幫助,appricate! – Razmo

+0

好,如果我的答案解決了您的問題,那麼接受它會很好。 –

0
public class converter { 

public static void main(String [] args) { 
    Options opt = new Options(); 

    opt.addOption("I", "in", true, "Eingabeformat (2,8,10,16)"); // Bei false gibts spaeter null 
    opt.addOption("O", "out", true, "Ausgabeformat (2,8,10,16)"); // Bei false gibt spaeter null 
    opt.addOption("V", "value", true, "Zu konvertierende Zahl"); 

    CommandLineParser parser = new DefaultParser(); 
    String value = "0"; 
    String in, out; 
    int inInt = 10; 
    int outInt = 10; 

    try { 
     CommandLine cl = parser.parse(opt, args); 

     if (cl.hasOption("I")) { 
      in = cl.getOptionValue("I"); 
      inInt = Integer.parseInt(in); 
     } else if (cl.hasOption("in")) { 
      in = cl.getOptionValue("in"); 
      inInt = Integer.parseInt(in); 
     } 

     if (cl.hasOption("O")) { 
      out = cl.getOptionValue("O"); 
      outInt = Integer.parseInt(out); 
     } else if (cl.hasOption("out")) { 
      out = cl.getOptionValue("out"); 
      outInt = Integer.parseInt(out); 
     } 

     if (cl.hasOption("V")) { 
      value = cl.getOptionValue("V"); 
      System.out.println(convert(value, inInt, outInt)); 
     } else if (cl.hasOption("value")) { 
      value = cl.getOptionValue("value"); 
      System.out.println(convert(value, inInt, outInt)); 
     } else { 
      System.out.println("Keinen Wert eingegeben. Erneut Versuchen!"); 
     } 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } 

} 

public static String convert(String input, int srcRadix, int dstRadix) { 
    int value = Integer.parseInt(input, srcRadix); 
    return Integer.toString(value, dstRadix); 
} 

}