0

I am beginner in Java. I wrote a few lines of code, and it is showing error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

import java.util.Scanner;
public class Issue {
    public static Scanner scan = new Scanner(System.in);
    int n;
    int ID[]=new int[n];
    String [] Name=new String[n];
    public void get()
    {

        int ID[] = new int[n];
        for (int i = 0; i < n; i++) {
            System.out.println("Enter " + (i+1) + "st Employe ID :");
            ID[i] = scan.nextInt();
            System.out.println("Enter Employe Name:");
            Name[i]=scan.nextLine();

        }
    }
    public static void main(String[] args) {
        Issue obj=new Issue();
        System.out.println("Enter no.of Employes:");
        obj.n=scan.nextInt();
        obj.get();

    }
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
  • use `next()` instead of `nextLine()`. – Nisarg Jul 30 '18 at 16:37
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Laurenz Albe Jul 30 '18 at 16:43
  • yes @Nisarg that works but for string we should use nextLine() right?. what the issue with nextLine() in my code – barre konda Jul 30 '18 at 17:16

3 Answers3

0

At the time you have created the object of class Issue, the value of n was 0 and so you have created the arrays of ID and Name with size 0. Now after you have taken the input from user, you have only set n to the user input but the ID[] and Name[] arrays still have size 0. So inside the get method you are accessing out of box indexes in the 0-size arrays.

Here is the corrected code:

import java.util.Scanner;
public class Issue {
    public static Scanner scan = new Scanner(System.in);
    int n;
    int ID[];
    String [] Name;
    public void get()
    {
        for (int i = 0; i < n; i++) {
            System.out.println("Enter " + (i+1) + "st Employe ID :");
            ID[i] = scan.nextInt();
            System.out.println("Enter Employe Name:");
            Name[i]=scan.nextLine();
        }
    }
    public static void main(String[] args) {
        Issue obj=new Issue();
        System.out.println("Enter no.of Employes:");
        obj.n=scan.nextInt();
        obj.Name =new String[n];
        obj.ID = new int[n];
        obj.get();

    }
}

In above code I have only made correction for ArrayIndexOutOfBound. However your code can be improved by following good programming practices like these:

  1. Follow the naming conventions of methods and variables.
  2. Make the non-final member objects of a class private and use getters and setters.
  3. Follow separation of concern rule.
  4. Use BufferedReader instead of Scanner for faster performance.
utsav_deep
  • 621
  • 6
  • 23
  • iam unable understand but ur code is works . I updated n value with user input right then why ID[] and Name[] has 0 index. – barre konda Jul 30 '18 at 16:54
  • @barrekonda Thats because you only updated the value of n from 0 to possibly some positive integer. However, ID and Name array were initialised earlier effectively like this: `int ID[] = new int[0]` `String Name[] = new String[0]` However you havent made the new arrays according to the new value of n. If you got it and if the code works, kindly accept and upvote my answer. – utsav_deep Jul 30 '18 at 17:02
0

It's a bit tricky, because although you set with obj.n=scan.nextInt(); a number to n, the array's size remains 0 since it has been initialized with the dafult n value 0.

The line:

obj.n=scan.nextInt();

Doesn't assure the reallocation of memory for the arrays ID and Name. I suggest you to avoid public variables and use the constructor for the total number encapsulation.

public static final Scanner scan = new Scanner(System.in);

private final int n;
private final int ID[];
private final String[] Name;

public Main(int number) {
    this.n = number;
    this.ID = new int[n];
    this.Name = new String[n];
}

public void get() {

    for (int i = 0; i < n; i++) {
        System.out.println("Enter " + (i+1) + "st Employe ID :");
        ID[i] = scan.nextInt();
        scan.nextLine();
        System.out.println("Enter Employe Name:");
        Name[i] = scan.nextLine();

    }
}

public static void main(String[] args) {
    System.out.println("Enter no.of Employes:");
    Main obj = new Main(scan.nextInt());
    obj.get();
}

Moreover, you have to call scan.nextLine(); to consume the line itself, because Scanner::nextInt doesn't terminate the line the same Scanner::nextLine does.

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

Occurs when you attempt to get element at an index greater than size of the array.

For Example,

you have an array A of size 10 and you are accessing A[10] then you will get exception because

  • Array size is 10 so valid indexes are 0-9 to access array element
  • A[10] means accessing (10+1)th element that is greater than array size(10)

In your case you have initialized arrays ID and Name at time when variable n is having value 0. To resolve this error you should initialize these arrays after variable n is initialized.

Kesha
  • 497
  • 6
  • 19