2

The program compiles but it gives the following error:

Two
One
Three
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at testingN.printArray(testingN.java:19)
at testingN.main(testingN.java:10)

I am really new into Java ( and programming ) and I'm stuck with this.

public class testingN 
{
    public static void main(String[] args) 
    {
        String[] names = new String[3];
        names[0] = "Two";
        names[1] = "One";
        names[2] = "Three";
        printArray(names);
    }
    public static String printArray(String[] data) 
    {
        int i;
        for (i = 0; i < data.length; i++) 
        {
          System.out.println(data[i]);
        }
        return data[i];
    }
}
6
  • 4
    return data[i]; is using index 3, because i is incremented before the loop exits. Commented Oct 28, 2013 at 18:28
  • Like @jonhopkins said, if you want to return the last element of the array you should return data[i-1];. That said, a method that prints should be declared as void (not return anything). If you want to implement another functionality you should do it somewhere else... Commented Oct 28, 2013 at 18:29
  • Do you really need to return anything? It seems that you could just make the method void and take out the return statement. Commented Oct 28, 2013 at 18:29
  • Could you please implement a simple functionality somewhere else (new class or ?) for me just to see an example because I am really getting confused? :( I do not want to use void method, but a return method ! Commented Oct 28, 2013 at 18:35
  • To be honest, what I really need to do is to create an array of numbers (input with scanner) and given this array I want to return another array with new numbers(I have to use some formula in the return method to convert the numbers) Commented Oct 28, 2013 at 18:38

2 Answers 2

2

It's because of the way a for loop works.

for (*initialization*; *some boolean condition*; *action to perform each loop*) {
    //...
}

A for loop works as follows:

  1. It begins by running the code in the initialization section. In your case, it sets i = 0
  2. It runs the contents of the loop
  3. It performs the action. In your case i++.
  4. It checks the boolean condition. If it is true, it goes to step 2. Otherwise it exits the loop. In your case the condition is i < data.length.

Since step 3 happens before the condition is checked, your i value is going to be 1 larger than the array size -1. That is, it will be the index exactly one larger than the last index of the array. So return data[i]; gives you an out of bounds exception.

Sign up to request clarification or add additional context in comments.

Comments

0
public class testingN 
{
    public static void main(String[] args) 
    {
        String[] names = new String[3];
        names[0] = "Two";
        names[1] = "One";
        names[2] = "Three";
        printArray(names);
    }
    public static void printArray(String[] data) 
    {
        for (int i = 0; i < data.length; i++) 
        {
          System.out.println(data[i]);
        }
    }
}

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.