Thread: Arguments

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

    Join Date
    Feb 2018
    Posts
    1
    Rep Power
    0

    Question Arguments


    Hello,
    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
    arguments.


    Code:
    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

    Code:
    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at com.java24hours.NewRoot2.main(NewRoot2.java:12)
    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
    Location
    Eastern Florida
    Posts
    3,737
    Rep Power
    352
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2018
    Posts
    6
    Rep Power
    0
    Originally Posted by rushed91
    Hello,
    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
    arguments.


    Code:
    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

    Code:
    run:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    	at com.java24hours.NewRoot2.main(NewRoot2.java:12)
    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

    The argument is the begining of the array starting at index 0.

    I've rendered your code and it looks like there is something wrong with the class or package of the file...

    https://gist.github.com/768f211a47da...2c93d4a79e2ec9

    https://stackoverflow.com/questions/...ain-class-mean

    I've encountered a similar problem to this before. It's basicually saying either the current array goes over the array's limit or is starting where the array does not. You could try a I/O Exception catch and see if it comes with a different result.

IMN logo majestic logo threadwatch logo seochat tools logo