0
import java.util.Scanner;  //importing scanner to get user input

public class ArrayHelper
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int[] hello = new int[10];
        for(int i = 0; i < 10; i++) //to get right number of integers
        {
            System.out.print("Please enter an integer: ");
            hello[i] = input.nextInt();
        }
//printing everything out 
        display(hello);
        System.out.println();
        System.out.print("Evens: ");
        display(onlyEvens(hello));
        System.out.println();
        System.out.print("Positives: ");
        display(onlyPositives(hello));
        System.out.println();
        System.out.print("Odds: ");
        display(disjoint(hello ,onlyEvens(hello)));
        System.out.println();
        System.out.print("Negatives: ");
        display(disjoint(hello ,onlyPositives(hello)));
        System.out.println();
    }

    public static void display(int[] nums)
    {
        for(int i = 0; i < nums.length -1; i++)
            System.out.print(nums[i] + ", ");

        System.out.print(nums[nums.length - 1]); //!!!!!!this is where the error occurs
    }
    public static int[] onlyEvens(int[] nums) //only even numbers in array (anything divisible by 2 including 0)
    {
        int x = 0;  //for set length
        for(int i = 0; i < nums.length; i++)
            if (nums[i]%2 == 0) //checks if even
                x++;

        int[] y = new int[x];
        int z = 0;

        for(int i = 0; i < nums.length; i++)
            if (nums[i]%2 == 0) //checks if even
            {
                y[z] = nums[i];
                z++;
            }
        return y;
    }
    public static int[] onlyPositives(int[] nums) //looking for only positive integers in array
    {
        int x = 0;  //sets set length
        for(int i = 0; i < nums.length; i++)
            if (nums[i] > -1)   //checks if positive
                x++;

        int[] y = new int[x];
        int z = 0;

        for(int i = 0; i < nums.length; i++)
            if (nums[i] > -1)   //checks if positive
            {
                y[z] = nums[i];
                z++;
            }

        return y;
    }

    public static int[] disjoint(int[] nums, int[] nums2)
    {
        int x = 0;

        for(int i = 0; i < nums.length; i++)
        {
            int j = nums[i];
            if(!contains(nums2 , j))    //checks if letter be there
                x++;
        }

        for(int i = 0; i < nums2.length; i++)
        {
            int j = nums2[i];   //checks if letter be there
            if(!contains(nums , j))
                x++;
        }

        int[] y = new int[x];
        int z = 0;

        for(int i = 0; i < nums.length; i++)
        {
            int j = nums[i];    //checks if letter be there
            if(!contains(nums2 , j))
            {
                y[z] = nums[i];
                z++;
            }
        }

        for(int i = 0; i < nums2.length; i++)
        {
            int j = nums2[i];   //checks if letter be there
            if(!contains(nums , j))
            {
                y[z] = nums2[i];
                z++;
            }
        }
        return y;
    }
}

this is a program that takes in user input to get 10 integers to create an array. I have all of the program done, and there are no compile time errors, but when I run the code, there's an index out of bounds (-1) problem in the display method and ive done everything to try and fix it. thank you!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • have you tried running through it with a debugger? – ControlAltDel Feb 07 '17 at 21:40
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Ole V.V. Feb 07 '17 at 22:10
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – chris g Feb 08 '17 at 02:32

2 Answers2

0

It seems you forgot to create the contains method.

Here it is the method:

private static boolean contains(int[] nums, int j) {
        return false;
    }

And here it is your entire code tested and it seems it is working:

import java.util.Scanner;  //importing scanner to get user input

public class OutOfBound {

        public static void main(String[] args)
        {
            Scanner input = new Scanner(System.in);
            int[] hello = new int[10];
            for(int i = 0; i < 10; i++) //to get right number of integers
            {
                System.out.print("Please enter an integer: ");
                hello[i] = input.nextInt();
            }
    //printing everything out 
            display(hello);
            System.out.println();
            System.out.print("Evens: ");
            display(onlyEvens(hello));
            System.out.println();
            System.out.print("Positives: ");
            display(onlyPositives(hello));
            System.out.println();
            System.out.print("Odds: ");
            display(disjoint(hello ,onlyEvens(hello)));
            System.out.println();
            System.out.print("Negatives: ");
            display(disjoint(hello ,onlyPositives(hello)));
            System.out.println();
        }

        public static void display(int[] nums)
        {
            for(int i = 0; i < nums.length -1; i++)
                System.out.print(nums[i] + ", ");

            System.out.print(nums[nums.length - 1]); //!!!!!!this is where the error occurs
        }
        public static int[] onlyEvens(int[] nums) //only even numbers in array (anything divisible by 2 including 0)
        {
            int x = 0;  //for set length
            for(int i = 0; i < nums.length; i++)
                if (nums[i]%2 == 0) //checks if even
                    x++;

            int[] y = new int[x];
            int z = 0;

            for(int i = 0; i < nums.length; i++)
                if (nums[i]%2 == 0) //checks if even
                {
                    y[z] = nums[i];
                    z++;
                }
            return y;
        }
        public static int[] onlyPositives(int[] nums) //looking for only positive integers in array
        {
            int x = 0;  //sets set length
            for(int i = 0; i < nums.length; i++)
                if (nums[i] > -1)   //checks if positive
                    x++;

            int[] y = new int[x];
            int z = 0;

            for(int i = 0; i < nums.length; i++)
                if (nums[i] > -1)   //checks if positive
                {
                    y[z] = nums[i];
                    z++;
                }

            return y;
        }

        public static int[] disjoint(int[] nums, int[] nums2)
        {

            int x = 0;

            for(int i = 0; i < nums.length; i++)
            {
                int j = nums[i];
                if(!contains(nums2 , j))    //checks if letter be there
                    x++;
            }

            for(int i = 0; i < nums2.length; i++)
            {
                int j = nums2[i];   //checks if letter be there
                if(!contains(nums , j))
                    x++;
            }

            int[] y = new int[x];
            int z = 0;

            for(int i = 0; i < nums.length; i++)
            {
                int j = nums[i];    //checks if letter be there
                if(!contains(nums2 , j))
                {
                    y[z] = nums[i];
                    z++;
                }
            }

            for(int i = 0; i < nums2.length; i++)
            {
                int j = nums2[i];   //checks if letter be there
                if(!contains(nums , j))
                {
                    y[z] = nums2[i];
                    z++;
                }
            }
            return y;
        }

        private static boolean contains(int[] nums, int j) {
            return false;
        }
    }
zypa
  • 303
  • 2
  • 13
0

Your display method does not handle the case when the given array nums is empty (i.e. it contains zero elements). When the array is empty, nums.length==0, so nums[nums.length - 1] is nums[-1] which creates the exception.

Why do you leave out the last element from the loop? Your loop handles empty arrays correctly, so let the loop print all the elements:

public static void display(int[] nums)
{
    for(int i = 0; i < nums.length; i++)
        System.out.print(nums[i] + ", ");
}
  • He probably left the last one outside of the loop because he doesn't want the comma and space at the end. – EyedJellyfish Feb 07 '17 at 21:50
  • 1
    That's possible. If you don't want to occupy yourself with the formatting, you can use [`Arrays.toString`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString%28java.lang.Object[]%29). – Holger Jaekel Feb 07 '17 at 22:02