How to solve ArrayIndexOutOfBoundsException in this code?

maryt

I'm just working on a easy easy program with for loop and while loop, and an ArrayIndexOutOfBoundsException occurred.

Here is my code:

public class ForWhileLoops
{
   public static void main(String[] args)
   {
     int[] mary = new int[30];

     for(int a = 0; a < 31; a++)
     {
      mary[a]= a*3;
     }
     for(int b = 0; b < 31; b++)
     {
       System.out.println(mary);
     }
     int c = 0;
     while(c < 31)
     {
       c++;
       System.out.println(c);
     }
  }
}

And here is the error that occurred:

java.lang.ArrayIndexOutOfBoundsException: 30
    at ForWhileLoops.main(ForWhileLoops.java:9)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
bznein
int[] mary = new int[30];

Array are indexed starting from 0.

So an int[30] array will have valid indices from 0 to 29

for(int a = 0; a < 31; a++)

in the last iteration you are accessing mary[30] which is out of the bounds of your array.

Fix this by replacing it with

for(int a = 0; a <mary.length; a++)

With this solution if you change the size of your array you don't have to change the for loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to solve ArrayIndexOutOfBoundsException in this code?

From Dev

How to solve ArrayIndexOutofBoundsException

From Dev

How to solve java.lang.ArrayIndexOutOfBoundsException in java?

From Dev

How to solve java.lang.ArrayIndexOutOfBoundsException error

From Dev

how can i solve java.lang.ArrayIndexOutOfBoundsException error

From Dev

Cannot solve java.lang.ArrayIndexOutOfBoundsException

From Dev

Proguard: how to solve error code 1?

From Dev

How to solve SonarQube complaints about my code?

From Dev

How to solve javax.naming.NameNotFoundException at this code

From Dev

How can i solve duplication of code in this?

From Dev

How to solve SonarQube complaints about my code?

From Dev

How to solve javax.naming.NameNotFoundException at this code

From Dev

Proguard: how to solve error code 1?

From Dev

How can I solve the errors in this DirectX code?

From Dev

How to solve this cmake issue with c++ code?

From Dev

How to solve this "LocalProcessingException was unhandled by user code"?

From Dev

How do i solve this code in JSP?

From Dev

how to change this code to be able to solve this challenge?

From Dev

How to solve function overloading ambiguity in following code

From Dev

Histogram project: code throws ArrayIndexOutOfBoundsException

From Dev

I get an ArrayIndexOutOfBoundsException in my code

From Java

How to prevent an ArrayIndexOutOfBoundsException in this case?

From Dev

How to handle ArrayIndexOutOfBoundsException in a game

From Dev

What does ArrayIndexOutOfBoundsException mean and how do I get rid of it? Here is a code sample that triggers the exception:

From Dev

How to fix the ArrayIndexOutOfBoundsException in Adapter android

From Dev

How to solve error generate python3 code in ANTLR 4.4 ?

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Java

How to solve npm error “npm ERR! code ELIFECYCLE”

From Dev

How to solve this compiling error with pqxx under code blocks

Related Related

  1. 1

    How to solve ArrayIndexOutOfBoundsException in this code?

  2. 2

    How to solve ArrayIndexOutofBoundsException

  3. 3

    How to solve java.lang.ArrayIndexOutOfBoundsException in java?

  4. 4

    How to solve java.lang.ArrayIndexOutOfBoundsException error

  5. 5

    how can i solve java.lang.ArrayIndexOutOfBoundsException error

  6. 6

    Cannot solve java.lang.ArrayIndexOutOfBoundsException

  7. 7

    Proguard: how to solve error code 1?

  8. 8

    How to solve SonarQube complaints about my code?

  9. 9

    How to solve javax.naming.NameNotFoundException at this code

  10. 10

    How can i solve duplication of code in this?

  11. 11

    How to solve SonarQube complaints about my code?

  12. 12

    How to solve javax.naming.NameNotFoundException at this code

  13. 13

    Proguard: how to solve error code 1?

  14. 14

    How can I solve the errors in this DirectX code?

  15. 15

    How to solve this cmake issue with c++ code?

  16. 16

    How to solve this "LocalProcessingException was unhandled by user code"?

  17. 17

    How do i solve this code in JSP?

  18. 18

    how to change this code to be able to solve this challenge?

  19. 19

    How to solve function overloading ambiguity in following code

  20. 20

    Histogram project: code throws ArrayIndexOutOfBoundsException

  21. 21

    I get an ArrayIndexOutOfBoundsException in my code

  22. 22

    How to prevent an ArrayIndexOutOfBoundsException in this case?

  23. 23

    How to handle ArrayIndexOutOfBoundsException in a game

  24. 24

    What does ArrayIndexOutOfBoundsException mean and how do I get rid of it? Here is a code sample that triggers the exception:

  25. 25

    How to fix the ArrayIndexOutOfBoundsException in Adapter android

  26. 26

    How to solve error generate python3 code in ANTLR 4.4 ?

  27. 27

    How to solve the Total of my html code that linking with input type = "number"

  28. 28

    How to solve npm error “npm ERR! code ELIFECYCLE”

  29. 29

    How to solve this compiling error with pqxx under code blocks

HotTag

Archive