bubai93
bubai93

Reputation: 51

Java pass input to a function from terminal

Is there any way I can write a command in the terminal like

config.group1.val1

and somehow parse this command and send "group1" and "val1" as two parameters in a Java function?

I can't send it in main function through args[] array.

Upvotes: 0

Views: 174

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 45005

It can be done as next:

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);

Upvotes: 2

Related Questions