Hello,

I was hoping to get some input from anyone in the Java section. My professor gave us a practice problem about two weeks ago and I have been running into an out of bounds error. The program seemed simple enough, take in two arrays of binary numbers from a user, add them, and return the sum as a third array.

So far I have this:

import java.util.*;

public class Binary
{
    public static int[] calculate(int[] arr1, int[] arr2)
    {
        int length = (arr1.length + arr2.length);
        int carry = 0;

        int[] newArray = new int[length];

        for(int i = 0; i < newArray.length; i++)
        {
                if((arr1[i] == 1) && (arr2[i] == 1))
                {
                    newArray[i] = 0;
                    carry = 1;
                }
                if((arr1[i] == 1) && (arr2[i] == 1) && carry == 1)
                {
                        newArray[i] = 1;
                        carry = 1;
                }
            if((arr1[i] == 1) && (arr2[i] == 0))
            {
               newArray[i] = 1;
            }
            if((arr2[i] == 1) && (arr1[i] == 0))
            {
               newArray[i] = 1;
            }
        }
        return newArray;
    }

    public static boolean isValid(int val)
    {
        if(val == 0 || val == 1)
            return true;
        else
            return false;
    }

    public static void main(String[] args)
    {   
        Scanner keyboard = new Scanner(System.in);
        int length, length2;

        System.out.print("Enter a length for array 1: ");
        length = keyboard.nextInt();

      //First Array
        int[] array1 = new int[length];
        int temp;
        int counter = 0;


        System.out.print("Please enter 1 or 0 to represent a binary value\n"
                            +"--------------------------------------------------\n");

        while(counter < array1.length)
        {
            System.out.print("#"+(counter+1)+":");
            temp = keyboard.nextInt();

            if(isValid(temp) == true)
            {
                array1[counter] = temp;
                counter++;
            }
            else
                System.out.print("Incorrect value. Try again: ");
        }       
      counter = 0;

      System.out.print("Array #1: ");
      for(int i = 0; i < array1.length; i++)
         System.out.print(array1[i]+" ");


     System.out.println();


      //Second Array
      System.out.print("Enter a length for array 2: ");
      length2 = keyboard.nextInt();

      int[] array2 = new int[length2];

      System.out.print("Please enter 1 or 0 to represent a binary value\n"
                            +"--------------------------------------------------\n");

      while(counter < array2.length)
      {
         System.out.print("#"+(counter+1)+":");
         temp = keyboard.nextInt();

         if(isValid(temp) == true)
         {
            array2[counter] = temp;
            counter++;
         }
         else
            System.out.print("Incorrect value. Try again: ");
      }
      counter = 0;  

      System.out.print("Array #2: ");
      for(int i = 0; i < array2.length; i++)
         System.out.print(array2[i]+" ");

      System.out.println();   

       //Third Array                         (sum of array1 and array2)   
       int[] array3 = calculate(array1, array2);

       for(int i = 0; i < array3.length; i++)
         System.out.print(array3[i]+" ");
    }
}

But, I seem to run in to an array out of bounds error. I thought if I set the length to the sum of both array 1 and array 2 it would suffice. Also, instead of if-statements what are other suggestions to make this program more efficient?

Recommended Answers

All 3 Replies

On line 7 you are adding the lengths of your two array to sum together.
With that length you are going to loop through this two smaller arrays. The perfect failure scenario! I would take the Min of the two array lengths to add and work with that.

The result could be as long as the longer input aray, or 1 digit longer than that (if you end up witha carry), so maybe the easiest way is to set up 3 arrays of length (longest input +1), read the inputs into two of them and calc the result into the third. With all the arrays the same length you will have no problems with indexes or loops.

ps: a boolean expression evaluates to true or false, so some of your code is highly redundant, eg consider
if(isValid(temp) == true)
isValid(temp) returns true or false, so the == true does exactly nothing. All you need is
if (isValid(temp))

Similarly, consider

        if(val == 0 || val == 1)
            return true;
        else
            return false;

now val == 0 || val == 1 evaluates to true or false, so all you need is

return  val == 0 || val == 1;

@ddanbe
You are very right. And thank you for the suggestions. So you're saying that by taking the minimum value and working my way up, I should then in another section carry the remaining values to the empty slots of my third array?

@JamesCherrill
Thank you for you suggestions as well. I made the sections you mentioned a bit cleaner:

while(counter < array2.length)
  {
     System.out.print("#"+(counter+1)+":");
     temp = keyboard.nextInt();

     if(isValid(temp))
     {
        array2[counter] = temp;
        counter++;
     }
     else
        System.out.print("Incorrect value. Try again: ");
  }
public static boolean isValid(int val)
    {
        return val == 0 || val == 1;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.