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 13 of 13

Thread: Interger.parseInt Question

  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Interger.parseInt Question

    Hi,

    I copied this code from a book, any while it will compile, I get an exception error when I try to run it. Any idea what the issue is?

    class Power
    {
    public static void main ( String[] args )
    {
    int num = Integer.parseInt( args[0] ) ;
    int square = (int) Math.pow( num, 2 ) ;
    int cube = (int) Math.pow( num, 3 ) ;
    int sqrt = (int) Math.sqrt( num ) ;
    System.out.println( num + "squared is " + square ) ;
    System.out.println( num + " cubed is " + cube ) ;
    System.out.println( "Square root of " +num+" is " + sqrt ) ;
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interger.parseInt Question

    I get an exception error
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interger.parseInt Question

    java.lang.ArrayIndexOutOfBoundsException:
    0
    at Power.main(power.java:5)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interger.parseInt Question

    The error message says that the array: args is empty (it does not have an element at index 0)
    The code should test the length of the args array and make sure it has some content before trying to get at any of its elements.

    The user executing the program needs to have an arg on the commandline following the classname for the java program to pass to the main() method in the args array.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interger.parseInt Question

    Thanks Norm,

    I appreciate the response, but could you explain that in laymans terms? That would really help. Sorry, but I'm just starting out with Java.

  6. #6
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Interger.parseInt Question

    Hi,

    When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. You need to pass arguments while you run this code as on this line no 5 : int num = Integer.parseInt( args[0] ) ; you are fetching arguments from the command line.
    so when you run the code using the command : java Power 2
    you will get the output as below:
    2squared is 4
    2 cubed is 8
    Square root of 2 is 1

  7. #7
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interger.parseInt Question

    can you please show me the exact code you used to correct it? That would really help me understand what I need to do. Thanks in advance.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interger.parseInt Question

    The user needs to enter:
    java Power 2
    to execute the program without any errors.

    If you want the program to catch errors
    Pseudo code:
    if length of the array passed to main() is < 1
    then print error message to user and exit the program
    else use the first arg of the array
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interger.parseInt Question

    Thanks for the reply Norm, Where exactly do I insert the line Java Power 2?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interger.parseInt Question

    That is the command line that should be used when the program is executed.
    How do you execute the program?
    Here's a sample of the console when I execute the java command:
    D:\JavaDevelopment\Testing\ForumQuestions9>java TestCode13 args-here
    aAB=D:\JavaDevelopment\Testing\ForumQuestions9
     
    D:\JavaDevelopment\Testing\ForumQuestions9>
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interger.parseInt Question

    I'm using Bluej

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Interger.parseInt Question

    Sorry, I have no idea how to use your IDE.
    Once the program is finished being developed, move it out of the IDE to execute it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Mar 2012
    Posts
    37
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Interger.parseInt Question

    Ok, that makes sense, I'll do that. Thanks for all your help Norm. I'm sure I'll be posting many, many more times in this forum, hehe.

Similar Threads

  1. Using char at to split up a interger
    By mooncowtime in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 26th, 2011, 08:55 AM
  2. Use of Integer.parseInt
    By tarkal in forum Java SE APIs
    Replies: 3
    Last Post: September 8th, 2011, 03:37 AM
  3. ParsingTo Interger Issue
    By gamsa in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 19th, 2011, 05:14 PM
  4. how to display combination of char and interger
    By bsrk315 in forum Java Theory & Questions
    Replies: 2
    Last Post: April 22nd, 2010, 08:09 AM
  5. Using Integer.parseInt
    By Fraz in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 07:50 AM