Results 1 to 9 of 9
  1. #1
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default Problem with statement "Integer.parseInt(args[0]);

    See below. Below the class is the error message at running.

    I use Netbeans. Saving the class goes ok. But at running it gives the error message. The problem seems to be at the statement "int peter3 = Integer.parseInt(agrs[0]);"



    class PeterTry1 {
    public static void main(String[] args) {
    String peter1 = "p";
    System.out.println(peter1);

    int peter3 = Integer.parseInt(args[0]);
    float peter4 = peter3;
    Float peter5 = new Float(peter4);
    Integer peter6 = peter5.intValue();
    System.out.println(peter6);

    }
    }


    run:
    p
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at PeterTry1.main(PeterTry1.java:6)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Your args array is empty. How do you start your program? Have you set the run arguments in netbeans?
    Run -> Set project configuration -> Run -> Input a argument ....

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

    Default

    Line 6 will tell you where the error is occurring. You appear to be trying to use an array that has no members, whose size is 0. The only array here is the args array. You seem to be trying to get the command line parameters in this program but have neglected to run it with command line parameters.

    Solution 1: pass in some parameters when you run this code.

    Solution 2: Try to understand what the error message is telling you for often the answer is right there.

  4. #4
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks for the hints!! I had entered "10", when I now entered just a one digit argument, it worked fine. Can that have been the reason?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by peterfgoossens@gmail.com View Post
    Thanks for the hints!! I had entered "10", when I now entered just a one digit argument, it worked fine. Can that have been the reason?
    You didn't write that code yourself did you? Otherwise you would've known what that args[0] was for and you wouldn't have been so surprised when the error happened.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Correction: I answered too quick. It was not the number of digits. It was choosing "Run Main Project" and not "Run File". I used to use "Run File".

    Why is that?

  7. #7
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    I did write it myself though. 1) I am probably the newest of all newbies; 2) I didn't understand the runtime error (and do still not understand it); 3) but by running it as "main project" and not as "file" the code accepts any type of argument.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    Quote Originally Posted by peterfgoossens@gmail.com View Post
    Correction: I answered too quick. It was not the number of digits. It was choosing "Run Main Project" and not "Run File". I used to use "Run File".

    Why is that?
    Run File is the equivalent of "java MyProg"...no extra arguments.
    Run Main Project runs the setup defined in the Run Main Project bit, which includes a section for adding arguments.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by peterfgoossens@gmail.com View Post
    I did write it myself though.
    I've got it: you fell asleep with your nose on the keyboard and accidentally typed:

    Java Code:
    int peter3 = Integer.parseInt(args[0]);
    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Integer.parseInt() error
    By niteangell21 in forum New To Java
    Replies: 4
    Last Post: 02-06-2011, 05:36 AM
  2. Integer.parseInt("5.843"); Error
    By Cemi in forum New To Java
    Replies: 3
    Last Post: 04-15-2010, 05:16 PM
  3. [SOLVED] Integer.parseInt() problem
    By eggmanpete in forum New To Java
    Replies: 18
    Last Post: 02-15-2009, 04:25 PM
  4. Integer.parseInt?
    By Exhonour in forum New To Java
    Replies: 4
    Last Post: 01-20-2009, 02:31 AM
  5. Problem with Integer.parseInt()
    By Hevonen in forum New To Java
    Replies: 2
    Last Post: 12-14-2008, 03:41 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
  •