Saturday, 15 July 2017

java - Access enums through command line




enum Child {
David(23),
Johnson(34),

Brackley(19);
}

int age;

Child(int age) {
this.age=age;
}

void getAge() {

return age;
}

public class Test {
public static void main(String args[]) {
---------------------
}
}



If I had to enter command line arguments, for eg. if I enter java Test David
Then it should print "23".



So how can we access enums through command line.. ? what should be written in main method?



Please explain..


Answer



You need to convert the String arg from the command line to an enum value.



Child c = Child.valueOf(args[0]);


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...