Java将输入传递给终端的函数

时间:2016-05-29 16:04:23

标签: java command-line

我有什么方法可以在终端中编写命令,如

  
    

config.group1.val1

  

并以某种方式解析此命令并发送" group1"和" val1"作为Java函数中的两个参数?

我无法通过args []数组在main函数中发送它。

1 个答案:

答案 0 :(得分:2)

可以按照下一步完成:

Scanner scanner = new Scanner(System.in);
// get the next line from the terminal
String line = scanner.nextLine();
// split it using . as separator
String[] params = line.split("\\.");
// Default value
String value = "unknown";
// Assuming that the name of your map is "map"
Map<String, String> subMap = map.get(params[1]);
if (subMap != null && subMap.containsKey(params[2])) {
    value = subMap.get(params[2]);
}
// print the value found
System.out.println(value);