"java.lang.ArrayIndexOutOfBoundsException: 1" when the size of the array > the index -


public static int[] allbetween()     {         scanner input = new scanner(system.in);         int first;         int last;          system.out.println("enter first number");         first = input.nextint();         system.out.println("enter last number");         last = input.nextint();          int[] between = {((last - first) + 1)};          for(int count = 0; count <= (last - first); count++)         {             between[count] = (first + count);         }          return between;     } 

i'm little rusty , dont't see issue here, have tried manually assigning size of array 100 , first , last 1 , 5 still returns same error.

any ideas?

this first post on stack on flow, please correct me if i'm posting in incorrect manner

the below statement:

int[] between = {((last - first) + 1)}; 

initializes array single element, value - last - first + 1

change to:

int size = last - first + 1; int[] between = new int[size]; 

and then, can change loop to:

for(int count = 0; count < size; ++count) 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

javascript - addthis share facebook and google+ url -

ios - Show keyboard with UITextField in the input accessory view -