# What's Integer.parseInt & explain what exactly is the main method!

• 07-16-2013, 09:07 AM
bboyinmartin
What's Integer.parseInt & explain what exactly is the main method!
Code:

```package javaapplication2; class DayCounter{ public static void main(String[] arguments){ int yearIn = 2013; int monthIn = 12; if (arguments.length >0) monthIn = Integer.parseInt(arguments[0]); if( arguments.length > 1) yearIn = Integer.parseInt(arguments[1]); System.out.println( monthIn + "/" + yearIn + " has " + countDays(monthIn, yearIn) + "days."); }```
This is just a part of the whole java file/program that I don't understand.
So my question is, what length are they referring to at:
Code:

```if (arguments.length < 0) if (arguments.length < 1) ?```
Why did the person choose 0 and 1?

Also, what is "Integer.parseInt" and does (arguments[0] ) mean the monthIn will be stored in the "String[] arguments"?
If so, then what's the point of that?

Code:

```monthIn = Integer.parseInt(arguments[0]); yearIn = Integer.parseInt(arguments[1]);```

I know that I have many questions, but please answer this question too:

When I type in statements and expressions in the main method, do they get stored inside the main method? (In the "String[] arguments" in this case)

I'm self-studying Java with a book that doesn't explain things very well.
Thanks!
• 07-16-2013, 09:27 AM
Toll
Re: What's Integer.parseInt & explain what exactly is the main method!
Well, first of all, the above code would crash if you don't supply it with arguments, and wouldn't do what you'd want it to do if you did. I'm suspecting you used "less than" instead of "greater than" (i.e. < instead of >). Why they chose 0 and 1? It's pretty simple, actually. To put the code into words, it says "If the number of arguments is greater than 0, then use the first argument". Same reasoning for the second one.

Integer.parseInt will try to turn a String into an integer (so a String containing "123" would turn into an integer with 123 as a value).

arguments is an array that holds all the arguments used when starting the program. So if you did "java DayCounter 2 3", the array would hold the values 2 and 3.

I'm not sure what you mean by the last question, but hopefully I've cleared the arguments variable up a bit for you at least.
• 07-16-2013, 09:31 AM
PhHein
Re: What's Integer.parseInt & explain what exactly is the main method!
Have you ever seen ath API docs? Java Platform SE 7
• 07-16-2013, 09:52 AM
JosAH
Re: What's Integer.parseInt & explain what exactly is the main method!
An array can't have less than zero elements so your first if-statement doesn't make sense; if you try to access element #i the array should have at least i+1 elements ...

kind regards,

Jos
• 07-16-2013, 10:10 AM
bboyinmartin
Re: What's Integer.parseInt & explain what exactly is the main method!
Sorry about the inequality sign. I was testing out what the heck it does.

if (arguments.length > 0)
if (arguments.length > 1)

So what do they actually do? what IS the length of the arguments. Is it the number of characters in the main method or what?
• 07-16-2013, 10:12 AM
gimbal2
Re: What's Integer.parseInt & explain what exactly is the main method!
no, its the number of arguments you pass. Ex:

Code:

`java -jar myapp.jar param1 param2 param3`
or

Code:

`java MyClass param1 param2 param3`
the length is 3, because there are three arguments (param1, param2, param3)
• 07-16-2013, 10:44 AM
bboyinmartin
Re: What's Integer.parseInt & explain what exactly is the main method!
But if I replace the 0 and 1 with numbers like 20000, it still worked.
Even though I'm pretty sure that there aren't that many parameters.
• 07-16-2013, 10:54 AM
Toll
Re: What's Integer.parseInt & explain what exactly is the main method!
But did it use the values in the parameters, or the default values used in the program?
• 07-16-2013, 11:20 AM
bboyinmartin
Re: What's Integer.parseInt & explain what exactly is the main method!
Well I doubt it
• 07-16-2013, 01:33 PM
JosAH
Re: What's Integer.parseInt & explain what exactly is the main method!
If you tested 'arguments.length < 20000' I can understand the behaviour of the program ... show us your code.

kind regards,

Jos
• 07-16-2013, 10:12 PM
bboyinmartin
Re: What's Integer.parseInt & explain what exactly is the main method!
Code:

```package javaapplication2; class DayCounter{     public static void main(String[] arguments){         int yearIn = 2013;         int monthIn = 12;                   if (arguments.length > 20000)             monthIn = Integer.parseInt(arguments[0]);         if( arguments.length > 20000)             yearIn = Integer.parseInt(arguments[1]);         System.out.println( monthIn + "/" + yearIn + " has "                 + countDays(monthIn, yearIn) + "days.");             }         static int countDays(int month, int year) {         int count = -1;         switch (month) {             case 1:             case 3:             case 5:             case 7:             case 8:             case 10:             case 12:                 count = 31;                 break;             case 4:             case 6:             case 9:             case 11:                 count = 30;                 break;             case 2:                 if ( year % 4 == 0 )                     count = 29;                 else count = 28;                 if (( year % 100 == 0) & year % 400 !=0)                     count = 28;         }                             return count;                                 }     }```

This is the whole file. The output was:
12/2013 has 31 days.
• 07-17-2013, 03:52 AM
Fubarable
Re: What's Integer.parseInt & explain what exactly is the main method!
Please use code tags when posting code. You've been asked to do this several times. You may edit your above post and add the tags.
• 07-17-2013, 07:48 AM
Toll
Re: What's Integer.parseInt & explain what exactly is the main method!
That would be the expected output, yes. The default values used are year 2013 and month 12, after all. Try reverting back to 0 and 1 and run the program again with 0, 1 and 2 arguments and see what happens!
• 07-17-2013, 09:22 AM
bboyinmartin
Re: What's Integer.parseInt & explain what exactly is the main method!
So I 've tried a lot of different numbers & it seems like the arguments.length is infinite?

I was able to do

Code:

` (arguments.length > 9223372036854775807L )`
doesn't that mean arguments.length is basically infinite? since
the length of arguments is larger than 9223372036854775807, but just can't be less than 0. Am I right?
• 07-17-2013, 09:24 AM
Toll
Re: What's Integer.parseInt & explain what exactly is the main method!
No, the arguments.length is not infinite. Its length is the number of arguments you supply. If you make that kind of a check though, the code inside the if-clause will never fire (i.e. in this case, it will always use the default values).
• 07-17-2013, 09:28 AM
gimbal2
Re: What's Integer.parseInt & explain what exactly is the main method!
Code:

```        int yearIn = 2013;         int monthIn = 12;```
No matter what nonsensical check you do on the length parameter, year and month are always going to be 2013/12. At least -try- to understand what is going on in stead of staying on your fruitless mission to prove that the array length property does not do what it is documented to do :/
• 07-17-2013, 09:33 AM
bboyinmartin
Re: What's Integer.parseInt & explain what exactly is the main method!
Then why did the person who wrote this file/program use the following command?:

Code:

` if( arguments.length > 0)`

This was in the book "Sams Teach Yourself Java in 21 days 7th edition."

What was the purpose of that part?
Also, if the statement next to the if is false, then shouldn't it not work?

I'm sorry if I making a fuss out of nothing. I'm just a person with excessive amount of curiosity.
• 07-17-2013, 09:45 AM
gimbal2
Re: What's Integer.parseInt & explain what exactly is the main method!
Is it so hard? That line of code translates to english: "Are there any program arguments?"