I am having alot of trouble within this task, i am unsure on how to use the args[] values correctly ? can someone please explain how to use these correctly.
args[] values
Resolved
Comments (7)
- Popular
- New
- Old
You must be signed in to leave a comment
Diego deFontenelle
10 March 2021, 15:37useful
Hi,
Be sure, that you are not the first one to find this task complecated.
Args are arrays of arguments...
+1
Guadalupe Gagnon
10 March 2021, 15:37solution
What do you want to know? It is a String array, no different than any other String array:
args[0] is the first element and can be any String
args[1] is the second element
args[2] is the third arg.... etc
or are you asking how to fill it with values? On the top right of IntelliJ, where the codegym buttons are, next to the hammer there is a pull down menu. In that menu is "Edit Configurations". Here you can pass program arguments to your program. These arguments fill the args[] array in the program when run. Make sure that you are adding the args to the correct program, "Edit Configurations" will have edit the configurations of the last program that you had run. I always make sure to run each program first before adding arguments so that I am adding them to the correct project.
It will split what you type by the space char, so if you type this into the program arguments:
this is only a test
the resulting array would contain 5 String elements of:
{"this", "is", "only", "a", "test"}
If you want an element to contain more than one word, then encase the words in quotation marks. So if you typed:
this "is only a" test
the resulting array would contain 3 String elements of:
{"this", "is only a", "test"}
+3
Guadalupe Gagnon
10 March 2021, 15:40
0
David Close
10 March 2021, 16:18
i think i was just being abit stupid, thank you for clearing some of this up.
0
Guadalupe Gagnon
10 March 2021, 16:51
Not stupid. The main method is just a method like any other method, it has one input parameter of a String[]. The only difference is that this is the method that is called when a program starts. It normally takes people a couple of months, when they are first learning programming, to actually realize this. I know in my case, personally, I didn't realize this for about that amount of time.
+2
David Close
10 March 2021, 16:56
yeh i dont think i understood what it really meant until this task, and you helped me clear it up, if im writing a switch/case statement i can refer to args[] within another class? I mean within the constructor for Person, the first element is still "-c"? then args[1] is the first parameter of the constructor and so on?
0
Guadalupe Gagnon
10 March 2021, 17:13useful
The args[] array will be a local variable only visible within the main method. You can pass its values around to other methods/constructors as arguments, but you can't just implicitly access it directly from outside of main. So, with "-c" args[1] would be the first argument to the Person constructor, while args[2] and args[3] are for the next 2 constructor arguments, but they are type String and will need to be converted to types Sex and Date to be used.
And yes, a switch case would work well in this task on the 1st value in args (index 0):
example for -c:
{"-c", "Washington", "m", "04 15 1990"}
-u:
{"-u", "1", "Washington", "m", "04 15 1990"}
-d:
{"-d", "1"}
-i:
{"-i", "1"}
+1