Results 1 to 4 of 4
  1. #1
    Thompson is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default Help with BinarytoDecimal programme

    Hello when i run this programme i get this error

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0


    anyone know where i am going wrong


    public class BinToDec {

    public static void main (String[] args) {

    double dec = 0;
    int index = 0;

    for (int i=args[0].length()-1; i>=0; i--) {
    if (args[0].charAt(i)=='1')
    dec = dec + Math.pow(2, index);
    index++;
    }

    System.out.println("The binary no. is: "+args[0]);
    System.out.println("Converting to decimal ==> "+dec);
    }

    }

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Have you passed this program command line arguements?

    Command-Line Arguments (The Java™ Tutorials > Essential Classes > The Platform Environment)

    Your error is due to this args[0], args has no size. It is empty.

  3. #3
    Thompson is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    thanks for trying to help

    ive tried to change the value of args but i still get same error ??


    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 99999

  4. #4
    Junky's Avatar
    Junky is online now Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default

    You have made changes to your code, get an error and expect us to fix it for you without showing us your new code.

    Alternatively you can work this out for yourself. ArrayIndexOutOfBoundsExceptions are thrown when you try to access an element that does not exist.
    Java Code:
    int[] array = new int[5];
    array[10] = 42;
    This creates an array with 5 elements. The indicies are 0 - 4. So attempting to access an element with an index less than 0 or greater than 4 throws an exception. Somewhere in your code you try to access element at 99999 ( or the one hundred thousandth element) but your array is smaller than that.

Similar Threads

  1. this programme dont work as I want Help me!!
    By pinar in forum New To Java
    Replies: 2
    Last Post: 10-26-2010, 11:27 PM
  2. cannot compile the programme
    By Roshini in forum New To Java
    Replies: 3
    Last Post: 09-06-2010, 11:02 AM
  3. Can I embed a console java programme on my site?
    By ngc0202 in forum New To Java
    Replies: 7
    Last Post: 08-10-2010, 08:14 PM
  4. add java programme on windowsXP right-click menu
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 07-22-2008, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •