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

Thread: Interactions problem with compiled program

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Interactions problem with compiled program

    This is my program in order to find two integers that are divisible either by 7 or 11.


    public class Assignment1 {

    public static void main(String[] args) {

    int a = Integer.parseInt(args[0]);
    int b = Integer.parseInt(args[1]);

    boolean divisible = ( a % 7 == 0) || (a % 11 == 0);
    System.out.println(divisible);

    boolean divisible2 = (b % 7 == 0) || (b % 11 == 0);
    System.out.println(divisible2);
    }

    }


    The program is compiled with no errors. However, this shows up in Interactions when I try to Run the program:

    run Assignment1
    java.lang.ArrayIndexOutOfBoundsException: 0
    at Assignment1.main(Assignment1.java:8)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
    >

    What does this mean? What am I missing?
    I am a beginner, so please add as much detail into the explanations as possible.


  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: Interactions problem with compiled program

    va.lang.ArrayIndexOutOfBoundsException: 0
    at Assignment1.main(Assignment1.java:8)
    At line 8 the code tries to access the first element (index=0) of an empty array.

    The code should test that there are elements in the array before trying to access them.
    The .length attribute can be used for that.

    The program is expecting some arguments on the command line when it is started:
    java Assignment1 <arg1> <arg2> ...<ARGN>
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Working when runned from netbeans not when compiled
    By Jedan_covik in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 8th, 2012, 11:23 AM
  2. DecimalFormat error when compiled
    By roofninja in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 11th, 2012, 10:48 PM
  3. Replies: 1
    Last Post: April 11th, 2012, 05:03 AM
  4. no error when compiled but when the program runs .. help this !!!
    By izzahmed in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 8th, 2011, 08:55 AM
  5. Program compiled/ran as required - FAILED project WHY??????????
    By MISSAJ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 23rd, 2010, 03:35 PM