Thread: Arguments

  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2018
    Rep Power

    Question Arguments

    It's not my first time here.
    I used to post here a while ago but I lost my other account information so here we go with a new account

    I started reading this book about Java (Java in 24 hours by Sams Teach Yourself
    I am trying to learn Java on my own. After several attempts, I'm back to it again using a
    a different method (1 hour per day) besides my full-time job.

    So let's get to it..
    In the book I was doing a simple exercise to display the square root of a number using java.
    That was fine and I had no problems but then as an exercise at the end of the chapter, i've been asked to do it using

    package com.java24hours;
    class NewRoot2 {
        public static void main(String[] arguments) {
            /* The main() method puts all arguments in an array of strings, here named
               arguments. The first argument is stored in array element arguments[0].
               Calling the method Integer.parseInt() with that element creates an 
               integer value from a string. */
            int number = Integer.parseInt(arguments[0]);
            System.out.println("The square root of "
                + number
                + " is "
                + Math.sqrt(number)
    The way they explained arguments in the book is that in NetBeans to go to project configuration
    and modify the arguments of the class directly there.
    When putting this into NetBeans and running it, I get errors

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at com.java24hours.NewRoot2.main(
    C:\Users\Joe\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)
    This isn't the first book I'm reading about java. Like I said, I had few attempts on learning Java on my own but I couldn't
    discipline myself enough so i ended up not finishing the book. This method is really confusing the h*ll out of me.
    This argument thing looks like an 'array' but argument[0] is defined nowhere in the document. The only way i've been told to is going
    into the project configuration and have it there.

    If anyone can unconfuse me, that would really help me as this time I'm really trying my best to go through this book
    Thanks in advance

    I got an image here of what i'm talking about also
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Eastern Florida
    Rep Power
    The program should check that the size of the array passed to main is greater than 0 before trying to use any of its contents.
    The length field: theArrayName.length returns the size of the array.

    I don't know anything about how to configure the IDE NB.

    To test your code you could open a command prompt and issue a command line with the needed args:
    java NewRoot2 123
    where 123 is the String that will be placed in the array passed to the main method.

IMN logo majestic logo threadwatch logo seochat tools logo