AMAPstudio

### Sidebar

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 the String[] parameter of main()
• if you have String[] args, the number of Strings in the array is args.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, two double values and a String, 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 an int…), write an error message in the terminal and throw an exception to the caller
• send numberOfTrees, xSize and ySize to createTrees ()
• write a file with the given fileName

A possible solution