简体   繁体   中英

Unable to scan the elements into array

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();

    }
}

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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM