-1

so I am a beginner in java. I am trying to create a program that tells me the even ints in the array. However, I keep getting the array out of bounds error:10 at line 29(where I sy "formula[even[b]]. Help please?

public class arrayone
{
    public static void main(String args [])
    {
        /* 
    tell me the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 1.
    */

    //declare an array
    int[] even= new int[10];

    //int b is for the forloop so that every number can be added
    int b;

    //

    //initialize the scanner, and enter prompt
    Scanner input= new Scanner(System.in);

    //enter prompt
    System.out.printf("Enter 10 random numbers\n");

    //make a forloop so every number they put in will be added to the array
    for(b=0;b<10;b++)
    {
    even[b]=input.nextInt();
    }

//then run the formula
formula(even[b]);
    }

    public static void formula(int a)
    {


        //use an if statement to see if the numbers in the array are odd or even.
        for(a=0;a<=10;a++)
        {
        if((a%2)==0)
        {
            System.out.printf("This number is even\n");
        }
        else
        {
            System.out.printf("This number isn't even\n");

        }
        }
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 2
    There should not be a `for` loop at all in `formula`. You are only expecting an `int` – Aidin Dec 20 '16 at 22:32
  • I am honestly lost at what you think you are doing. Your `formula` makes no sense at all – Aidin Dec 20 '16 at 22:35
  • Instead of sending each integer to formula why not just send the whole array like so? https://repl.it/Eu6c/1 – Sash Sinha Dec 20 '16 at 22:46

2 Answers2

1

Ok, let's start from the beginning

You read a bunch of inputs and store it in an array, so far so good.

Then you are trying to send only one value to the function formula by doing formula(even[b]). However, as @Sanjeev pointed out, your b = 10 at this moment because of the for loop just before, hence giving you array out of bounds.

And then in formula you are only expecting an int, but you take this int (a in your case) and just reassign it in the for loop which btw checks what numbers between 0 and 10 (inclusive) are even. Not what you wanted I think.

What you really want to do is either:

for(int i = 0; i < 10; i++) {
    formula(even[i]);
} 

public static void formula(int a)
{
    if((a%2)==0)
    {
        System.out.printf("This number is even\n");
    }
    else
    {
        System.out.printf("This number isn't even\n");
    }
}

or

formula(even);

public static void formula(int[] a)
{
    for(int i = 0; i < a.length(); i++) {
        if((a[i]%2)==0)
        {
            System.out.printf("This number is even\n");
        }
        else
        {
            System.out.printf("This number isn't even\n");
        }
    }   
}
Aidin
  • 1,230
  • 2
  • 11
  • 16
0
   for(b=0;b<10;b++)
    {
       even[b]=input.nextInt();
    }

After exiting the for-loop value of b will be 10 which is more than the capacity of your array even. Array indexes are 0-9. Hence the exception.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33