# Use of Integer.parseInt

• September 7th, 2011, 05:30 PM
tarkal
Use of Integer.parseInt
Ok, so I was doing an exercises today that asked me to create an application that would display every date in a given year. I solved the exercise however when I looked at the text book answer I found that they had acheived it using a different (and more elegant) solution. The code snipped below shows the start of their solution. The rest is done with a switch.

Code :

```... public static void main(String[] arguments) { int year = 2011; if (arguments.length > 0) year = Integer.parseInt(arguments[0]); for (int month = 1; month < 13; month++) for (int day = 1; day <= countDays(month, year); day++) System.out.println(month + "/" + day + "/" + year); } ...```

I completely understand everything except...

Code :

``` if (arguments.length > 0) year = Integer.parseInt(arguments[0]);```

My confusion here is 2 fold! Firstly this is the first time I have seen any reference to "arguments.length" (or args.length). What "argument" is this refering to? Where is the length being determined from? and why is it in an if statment to determine whether the value is greater than 0? The fact that it has square brakets after it ([]) in the second line suggests that its refering to an array, but none is declared.

Secondly... according to the API "Integer.parseInt(String)" parses the string argument as a signed decimal integer. But year is already an int variable anyway.
• September 7th, 2011, 05:54 PM
shia
Re: Use of Integer.parseInt
Quote:

My confusion here is 2 fold! Firstly this is the first time I have seen any reference to "arguments.length" (or args.length). What "argument" is this refering to? Where is the length being determined from? and why is it in an if statment to determine whether the value is greater than 0? The fact that it has square brakets after it ([]) in the second line suggests that its refering to an array, but none is declared.
The String array "arguments" is in the first line of the code. It holds any command line arguments in a String array.
Code :

`public static void main(String[] arguments) {`

Quote:

Secondly... according to the API "Integer.parseInt(String)" parses the string argument as a signed decimal integer. But year is already an int variable anyway.
The variable "year" may be an int, but the call to the parseInt method converts element 0 of the arguments array to a int.
• September 7th, 2011, 05:59 PM
Junky
Re: Use of Integer.parseInt
Quote:

Originally Posted by tarkal
My confusion here is 2 fold! Firstly this is the first time I have seen any reference to "arguments.length" (or args.length). What "argument" is this refering to? Where is the length being determined from? and why is it in an if statment to determine whether the value is greater than 0? The fact that it has square brakets after it ([]) in the second line suggests that its refering to an array, but none is declared.

What is the parameter to the main method? It is a String array called arguments. All arrays have a public instance variable called length that holds value equivalent to how many elements the array can hold.
Code java:

```int[] arr = new int[10]; System.out.println(arr.length);```
That will display 10.

So where does the String array parameter to the main method come from? The command line.
Code java:

```class CommandLineArguments { public static void main(String[] args) { for(int index = 0; index < args.length; index++) { System.out.println(args[index]); } } }```
Run the above program from the command line with the follow command: java CommandLineArguments one two three four

The output will be:
one
two
three
four

Quote:

Secondly... according to the API "Integer.parseInt(String)" parses the string argument as a signed decimal integer. But year is already an int variable anyway.
Yes year is an int and as explained above the elements stored in the arguments array are Strings. Therefore you need to parse the String into an int before you can store it in an int variable.
• September 8th, 2011, 03:37 AM
tarkal
Re: Use of Integer.parseInt
Thanks guys. Interestingly not one of my books actually explains the main method argument (at least not yet). They all just say to "accept it as this is how main methods are constructed".

Thanks for the little program Junky, it was really really helpfull. I had no idea that the string array parameter could be passed to the main method from the command line. It now all makes perfect sense, including why Interger.parseInt is present, given that the value being returned from the command line is being stored as a String.