Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Use of Integer.parseInt

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

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

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

     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.

  2. Default Related threads:


  3. #2
    Junior Member shia's Avatar
    Join Date
    Sep 2011
    Location
    Manchester, UK
    Posts
    19
    My Mood
    Nerdy
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Use of Integer.parseInt

    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.
    public static void main(String[] arguments) {

    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.

  4. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Use of Integer.parseInt

    Quote Originally Posted by tarkal View Post
    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.
    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.
    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

    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.
    Improving the world one idiot at a time!

  5. #4
    Member
    Join Date
    Sep 2011
    Posts
    63
    My Mood
    Confused
    Thanks
    7
    Thanked 0 Times in 0 Posts

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

Similar Threads

  1. doing arithmetic on Integer objects
    By gib65 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 30th, 2010, 09:22 PM
  2. Help with ArrayList<Integer>
    By shanklove in forum Collections and Generics
    Replies: 2
    Last Post: September 6th, 2010, 09:15 AM
  3. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM
  4. Hex to Integer Problem
    By TempSpectre in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 06:01 AM
  5. int and Integer
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: December 31st, 2009, 03:20 AM