Results 1 to 2 of 2
  1. #1
    xminiguyx is offline Member
    Join Date
    Dec 2011
    Posts
    1
    Rep Power
    0

    Exclamation java.lang.ArrayIndexOutOfBoundsException: 20

    I created a simple game program (im just starting to learn java) and when i run the program it gives me this error:

    java.lang.ArrayIndexOutOfBoundsException: 20
    at Naruto.main(Naruto.java:164)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:271)

    When i run the program, i choose the characters but then it gives me the error message, anyway here's my code (its still not done):


    import java.util.Scanner;

    public class Naruto
    {
    public static void main( String[] args )
    {
    Scanner input = new Scanner( System.in );
    String[] z;
    z = new String[6];
    String[] a;
    a = new String[6];
    int[] x;
    x = new int[7];
    int[] y;
    y = new int[10];
    int[] o;
    o = new int[10];
    System.out.println( " Select Character you would like to play as");
    System.out.println( " 1 = Naruto");
    System.out.println( " 2 = Sasuke");
    System.out.println( " 3 = Lady Tsunade");
    x[1] = input.nextInt();
    if(x[1] == 1)
    {
    z[1] = " Naruto";
    //Stats
    y[1] = 100;
    y[2] = 100;
    y[3] = 50;
    //moves
    y[4] = 40;
    y[5] = 75;
    y[6] = 15;
    y[7] = 40;
    y[8] = 25;
    y[9] = 50;
    z[2] = "1000 Years of pain";
    z[3] = "Rasengan";
    z[4] = "Punch";
    z[5] = "Charge Chakra";
    }
    else if(x[1] == 2)
    {
    z[1] = " Sasuke";
    //Stats
    y[1] = 70;
    y[2] = 100;
    y[3] = 65;
    //moves
    y[4] = 55;
    y[5] = 75;
    y[6] = 15;
    y[7] = 30;
    y[8] = 35;
    y[9] = 50;
    z[2] = "Fire Style: Grand Fireball";
    z[3] = "Chioridi";
    z[4] = "Punch";
    z[5] = "Charge Chakra";
    }
    else if(x[1] == 3)
    {
    z[1] = " Lady Tsunade";
    //Stats
    y[1] = 120;
    y[2] = 50;
    y[3] = 60;
    //moves
    y[4] = 25;
    y[5] = 60;
    y[6] = 20;
    y[7] = 35;
    y[8] = 20;
    y[9] = 35;
    z[2] = "Poison Needles";
    z[3] = "Sonic Punch";
    z[4] = "Punch";
    z[5] = "Charge Chakra";
    }
    System.out.println( " Who should your opponent be?");
    System.out.println( " 1 = Naruto");
    System.out.println( " 2 = Sasuke");
    System.out.println( " 3 = Lady Tsunade");
    x[2] = input.nextInt();
    if(x[2] == 1)
    {
    a[1] = " Naruto";
    //Stats
    o[1] = 100;
    o[2] = 100;
    o[3] = 50;
    //moves
    o[4] = 40;
    o[5] = 75;
    o[6] = 15;
    o[7] = 40;
    o[8] = 25;
    o[9] = 50;
    a[2] = "1000 Years of pain";
    a[3] = "Rasengan";
    a[4] = "Punch";
    a[5] = "Charge Chakra";
    }
    else if(x[2] == 2)
    {
    a[1] = " Sasuke";
    //Stats
    o[1] = 70;
    o[2] = 100;
    o[3] = 65;
    //moves
    o[4] = 55;
    o[5] = 75;
    o[6] = 15;
    o[7] = 30;
    o[8] = 35;
    o[9] = 50;
    a[2] = "Fire Style: Grand Fireball";
    a[3] = "Chioridi";
    a[4] = "Punch";
    a[5] = "Charge Chakra";
    }
    else if(x[2] == 3)
    {
    a[1] = " Lady Tsunade";
    //Stats
    o[1] = 120;
    o[2] = 50;
    o[3] = 60;
    //moves
    o[4] = 25;
    o[5] = 60;
    o[6] = 20;
    o[7] = 35;
    o[8] = 20;
    o[9] = 35;
    a[2] = "Poison Needles";
    a[3] = "Sonic Punch";
    a[4] = "Punch";
    a[5] = "Charge Chakra";
    }
    System.out.println( "Who should go first?");
    System.out.println( " 1 = You");
    System.out.println( " 2 = Opponent");
    x[3] = input.nextInt();
    System.out.println( a[1] + ": Come at me, bro");
    System.out.println( "You : I shall win");
    while ( y[1] > 0 && o[1] > 0 )
    {
    System.out.println( z[1] + " has " + y[1] + " hp" );
    System.out.println( z[1] + " has " + y[2] + " mp" );
    System.out.println( a[1] + " has " + o[1] + " hp" );
    if(x[3] == 1)
    {
    System.out.println( " Select attack");
    System.out.println( " 1 = " + z[2]);
    System.out.println( " 2 = " + z[3]);
    System.out.println( " 3 = " + z[4]);
    System.out.println( " 4 = " + z[5]);
    x[4]= input.nextInt();
    if(x[4]==1)
    {
    System.out.println( z[1] + " used " + z[2]);
    o[1]= o[1] - ((y[3]*y[4])/150);
    }
    else if(x[4]==2)
    {
    System.out.println( z[1] + " used " + z[3]);
    o[1]= o[1] - ((y[3]*y[5])/150);
    }
    else if(x[4]==3)
    {
    System.out.println( z[1] + " used " + z[4]);
    o[1]= o[1] - ((y[3]*y[6])/150);
    }
    else if(x[4]==4)
    {
    System.out.println( z[1] + " used " + z[5]);
    y[2]= y[2] + y[7];
    }
    x[3] = 2;
    }
    if(x[3] == 2)
    {
    for( x[5]=0; x[5]<1; x[5]++)
    {
    x[6]=(int)( 4 * Math.random()+1);
    if(x[6]==1)
    {
    System.out.println( a[1] + " used " + a[2]);
    y[1]= y[1] - ((o[3]*o[4])/150);
    }
    else if(x[6]==2)
    {
    System.out.println( a[1] + " used " + a[3]);
    y[1]= y[1] - ((o[3]*o[5])/150);
    }
    else if(x[6]==3)
    {
    System.out.println( a[1] + " used " + a[4]);
    y[1]= y[1] - ((o[3]*o[6])/150);
    }
    else if(x[6]==4)
    {
    System.out.println( a[1] + " used " + a[5]);
    o[2]= o[2] + o[7];
    }
    }
    x[3] = 1;
    }
    }

    if(y[1] > 0)
    {
    System.out.println( "You won the battle");
    System.out.println( a[1] + ": You got lucky this time");
    }
    else if(o[1] > 0)
    {
    System.out.println( a[1] + " won the battle");
    System.out.println( a[1] + ": You should train more before you fight me again");
    }

    }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,879
    Rep Power
    8

    Default Re: java.lang.ArrayIndexOutOfBoundsException: 20

    What exact line is that? What is the size of the array? How many indexes does that mean it has? Which index are you trying to access?

    Also, you should really be using an Object that encapsulates all of these values, not a mixed-type array.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. java.lang.ArrayIndexOutOfBoundsException
    By rajasohaibmaroof in forum AWT / Swing
    Replies: 15
    Last Post: 09-29-2011, 07:46 PM
  2. Java.lang.ArrayIndexOutOfBoundsException:0>=0
    By maitoti in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2011, 05:03 AM
  3. Java.lang.ArrayIndexOutOfBoundsException
    By Ladyjest1 in forum New To Java
    Replies: 1
    Last Post: 07-12-2010, 10:11 PM
  4. java.lang.ArrayIndexOutOfBoundsException: 6
    By pyat77 in forum Java Applets
    Replies: 6
    Last Post: 03-06-2010, 01:12 PM
  5. java.lang.ArrayIndexOutOfBoundsException
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 0
    Last Post: 02-13-2009, 07:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •