Question by Nighthawk0973: java.lang.ArrayIndexOutOfBoundsException? (Java Programming Error in Eclipse)?
So I’m making a programming language, and I had the following code:
————————–Start of main class—————————-
import java.util.Scanner;

class Main{
static int line = 1;
static Scanner Code = new Scanner(System.in);
static String[] Programming = new String[line];
static int i = 1;
static String[] UserInput = new String[line];
static Code methods = new Code();

public static void main(String[] args){
while (i == 1){
Programming[line] = Code.nextLine();
if (Programming[line].equals(“Program.End_Program;”)){
methods.testProgramming();
}
if (Programming[line].equals(“Print:”)){
UserInput[line] = Code.nextLine();
}
line = line + 1;
}
}
}
—————————–New Class———————————–
import java.util.Scanner;

public class Code extends Main{

Scanner Code = new Scanner(System.in);

public void testProgramming(){
if (Programming[1].equals(“Print:”)){
System.out.println(UserInput[1]);
}
}
}
——————–Compile/Run Results——————————–
Print:
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
at Main.main(Main.java:13)
—————–Back to question——————-
Where the line
Print:
is me testing out my programming language.

I’ve tried looking up the error type but no luck.

Can somebody please help me I’m using Eclipse on the Java Default package with JDK 1.6.0_24. (I won’t re-install JDK just to update to version 1.6.0_25. Only when JDK 7 comes out :D)

Thanks in Advance!
Edit: After that answer same thing. I changed the code to this:
——————————main class——————————————-
import java.util.Scanner;
import java.util.ArrayList;

class Main{
static int line = 0;
static Scanner Code = new Scanner(System.in);
static String[] Programming = new String[line];
static int i = 1;
static String[] UserInput = new String[line];
static Code methods = new Code();

public static void main(String[] args){
while (i == 1){
Programming[line] = Code.nextLine();
if (Programming[line].equals(“Program.End_Program;”)){
methods.testProgramming();
}
if (Programming[line].equals(“Print:”)){
UserInput[line] = Code.nextLine();
}
line = line + 1;
}
}
}
————–next class——————-
import java.util.Scanner;
import java.util.ArrayList;

public class Code extends Main{

Scanner Code = new Scanner(System.in);

public void tes

Best answer:

Answer by jyothiprasadb
as far as I can see you have declared the Programming as an array of 1 element.

However, when you try to access Programming[1] you are getting this error. That is because array index starts with 0 in Java, C and many other programming languages.

That is why the error: Array Index Out Of Bounds

Add your own answer in the comments!

One thought on “java.lang.ArrayIndexOutOfBoundsException? (Java Programming Error in Eclipse)?”

  1. I am not really sure what you want to achieve, but perhaps it is something like this:
    import java.util.Scanner;

    public class MainStay
    {
    static String userInput [] = null;

    public static void main (String [] args)
    {
    Scanner Code = new Scanner (System.in);
    Code methods = new Code ();

    System.out.print (“Maximum number of elements to input: “);
    int numElements = Code.nextInt ();
    userInput = new String [numElements];
    Code = new Scanner (System.in);
    String line = “”;
    int count = 0;

    System.out.println(“Enter End to stop”);

    while((count < numElements)&(!line.equals("End"))) { System.out.print ("Enter data: "); line = Code.nextLine (); if(!line.equals("End")) { userInput[count] = line; count++; } } methods.testProgramming (count); } } //-----------------------------New Class----------------------------------- import java.util.Scanner; public class Code extends MainStay { public void testProgramming (int siz) { int size = siz; for(int i = 0; i < size; i++) { System.out.println (userInput [i]); } } }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.