Results 1 to 18 of 18
Like Tree2Likes
  • 1 Post By Toll
  • 1 Post By Fubarable

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

  1. #1
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

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

    Java 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:
    Java 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?

    Java 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)

    Please answer my questions
    I'm self-studying Java with a book that doesn't explain things very well.
    Thanks!
    Last edited by bboyinmartin; 07-16-2013 at 10:10 AM. Reason: changed color tags to [code] ... [/code] tags

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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.
    bboyinmartin likes this.

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: What's Integer.parseInt & explain what exactly is the main method!

    Have you ever seen ath API docs? Java Platform SE 7
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default 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?

  6. #6
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: What's Integer.parseInt & explain what exactly is the main method!

    no, its the number of arguments you pass. Ex:

    Java Code:
    java -jar myapp.jar param1 param2 param3
    or

    Java Code:
    java MyClass param1 param2 param3
    the length is 3, because there are three arguments (param1, param2, param3)

  7. #7
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default 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.

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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?

  9. #9
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Re: What's Integer.parseInt & explain what exactly is the main method!

    Well I doubt it

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default Re: What's Integer.parseInt & explain what exactly is the main method!

    Java 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.
    Last edited by bboyinmartin; 07-17-2013 at 05:13 AM.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.
    DarrylBurke likes this.

  13. #13
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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!

  14. #14
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default 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

    Java 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?

  15. #15
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default 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).

  16. #16
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default Re: What's Integer.parseInt & explain what exactly is the main method!

    Java 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 :/

  17. #17
    bboyinmartin is offline Member
    Join Date
    Jul 2013
    Posts
    18
    Rep Power
    0

    Default 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?:

    Java 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.
    Last edited by bboyinmartin; 07-17-2013 at 09:41 AM.

  18. #18
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,076
    Rep Power
    6

    Default 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?"

Similar Threads

  1. Replies: 8
    Last Post: 07-05-2013, 07:13 AM
  2. Integer.parseInt error help
    By TheElder777 in forum New To Java
    Replies: 7
    Last Post: 10-21-2012, 09:49 AM
  3. Integer parseInt(str) question
    By prasads in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 10:10 PM
  4. Integer.parseInt() error
    By niteangell21 in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 05:36 AM
  5. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •