2
public static int[] allBetween()
    {
        Scanner input = new Scanner(System.in);
        int first;
        int last;

        System.out.println("Enter the first number");
        first = input.nextInt();
        System.out.println("Enter the 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 a little rusty and I dont't see the issue here, I have tried manually assigning the size of the array to 100 and first and last to 1 and 5 but it still returns the same error.

any ideas?

this is my first post on stack over flow, Please correct me if I'm posting in an incorrect manner

1
  • There's nothing wrong with your post, but before you post about runtime errors you should probably debug more. Commented Aug 15, 2013 at 15:23

4 Answers 4

4

The below statement:

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

initializes the array with just a single element, with the value - last - first + 1

Change it to:

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

And then, you can change your loop to:

for(int count = 0; count < size; ++count)
Sign up to request clarification or add additional context in comments.

Comments

3

Issue is:

int[] between = {((last - first) + 1)}; //initializes array with value

You have only one value in this array at index 0, and if last-first is greater than ZERO, you will end up having ArrayIndexOutOfBoundsException.

Read arrays tutorial for more information.

Comments

3

This line:

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

creates an array with a single element whose value is equal to ((last - first) + 1.

Use:

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

anyway, to iterate through it, you can use a nicer more readable/idiomatic construct:

for(int count = 0; count < between[length]; count++)
{
    between[count] = (first + count);
}

Remember that arrays are addressed and dimensioned by brackets, created explicitly with braces.

Also, between[count] = (first + count); looks suspicious. Make sure that's really what you want it to do, namely to set the countth element of between to first+count. That would just make an array filled with first, first+1, ....

Comments

3

you should replace

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

with

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

because your version always creates an array of length 1. See this for example:

int[] foo = {22};

is an int[] of length 1 and foo[0] is 22. Whereas

int[] bar = new int[33];

createas an array of length 33 where each index stores the default value 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.