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: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

    Really confused.. could someone give me an idiot's explanation? Other people weren't able to help and honestly, it seems to me that this code should work fine and it is kind of pissing me off, why can't i set result.num with the int three variable? i have the same result.num.set(0,three) line in another method and it works perfectly

    I'm not really sure why I keep getting this error message. I'm trying to add int three to result. I don't understand why it doesn't let me do this, it seems result would be empty when I try to put three into index size1-1.

    Error message: (Lines 452, 423, 11 are marked)

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.set(Unknown Source)
    at BigInt.singleDigitMultiply(BigInt.java:452)
    at BigInt.multiply(BigInt.java:423)
    at Dem.main(Dem.java:11)

    Portion of BigInt class:
    public BigInt multiply(BigInt B2) {
    BigInt result = new BigInt();
    BigInt B1 = this;   
    result = singleDigitMultiply(B1,B2); Line 423
        return result;
     
    }
     
    private BigInt singleDigitMultiply(BigInt B1, BigInt B2) {
        int size1 = B1.num.size(), size2 = B2.num.size();
        BigInt result = new BigInt();
        //result.num = new ArrayList (Collections.nCopies( size1+1, null ));
        boolean carryover = false;
        int one, two, three, four, five, carry;
        for (int i = size1-1; i >= 0; i--) {
     
             one = B1.num.get(i);
             two = B2.num.get(0); 
     
             int prod = B1.num.get(i) * B2.num.get(0);
             int prodInt = prod / 10;
             if (prod > 9) carryover = true;
     
             if (i == size1-1) {
     
             if (carryover) {
             three = prod % (prodInt * 10); 
             }
             else {
                three = prod;
             }
     
        System.out.println( result.num.set(size1-1,three));          Line 452
             }
     
        //   else {
            //   four = B1.num.get(i+1) * two;
        //       carry = four % (four - prodInt);
        //       if (four > 9) {
        //   
        //       five = prod + carry;
        //       three = five % (prodInt * 10);
        //       }
        //       else {
        //           three = prod;
        //       }
            //   result.num.add(i,three);
        //   }
     
     
     
     
        }
     
        return result;
    }

    Main class:

    public class Dem {
    public static void main(String[] args) {
    BigInt B1 = new BigInt("55");
    BigInt B2 = new BigInt("1");
    BigInt D = new BigInt();
    int u = 8;
    int j = 10;
     
    //BigInt J = new BigInt("u");
    D = B1.multiply(B2);           Line 11
    System.out.println(u/j);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

    Also posted at: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Exception in thread "main" java.lang.NoSuchMethodError: main?
    By Sakharam01 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 10th, 2013, 08:06 AM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  5. Replies: 2
    Last Post: March 26th, 2010, 11:22 AM