solution_of_8._pass_parameters_on_the_command_line
The Java training online
Solution: 8. Pass parameters on the command line
Try to work with the helping elements before looking at the solution…
Helping elements
- adapt the
main()
method of exercise 1 to process 4 parameters
- note that the 4 parameters will be stored in
Strings
, in theString[]
parameter ofmain()
- if you have
String[] args
, the number ofStrings
in the array isargs.length
- expected parameters:
int numberOfTrees, double xSize, double ySize, String fileName
- write them in the terminal with a loop
- interpret the values expected in order: one
int
value, twodouble
values and aString
, store them in 4 variables
- to turn a String containing digits into an int, use
new Integer(String).intValue ()
- to turn a String containing digits into a double, use
new Double(String).doubleValue ()
- in case of trouble during interpretation (e.g. expected an
int
and value is not anint
…), write an error message in the terminal and throw an exception to the caller
- send
numberOfTrees
,xSize
andySize
tocreateTrees ()
- write a file with the given
fileName
A possible solution
solution_of_8._pass_parameters_on_the_command_line.txt · Last modified: 2021/12/13 09:28 by 127.0.0.1