[英]Unable to scan the elements into array
I am beginner in Java. 我是Java的初学者。 I wrote a few lines of code, and it is showing error:
我写了几行代码,它显示了错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
线程“主”中的异常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. 在创建类Issue的对象时,n的值为0,因此创建了大小为0的ID和Name数组。现在,从用户那里获取输入后,只需将n设置为用户输入,但ID []和Name []数组的大小仍然为0。因此,在get方法内部,您将访问0大小数组中的开箱即用索引。
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. 在上面的代码中,我仅对ArrayIndexOutOfBound进行了更正。 However your code can be improved by following good programming practices like these:
但是,可以通过遵循以下良好的编程习惯来改进您的代码:
It's a bit tricky, because although you set with obj.n=scan.nextInt();
这有点棘手,因为尽管您使用
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. n
的数字,因为数组的大小已用dafult n
值0初始化,所以其大小保持0
0。
The line: 该行:
obj.n=scan.nextInt();
Doesn't assure the reallocation of memory for the arrays ID
and Name
. 不保证为数组
ID
和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();
此外,您必须调用
scan.nextLine();
to consume the line itself, because Scanner::nextInt
doesn't terminate the line the same Scanner::nextLine
does. 消耗行本身,因为
Scanner::nextInt
不会像Scanner::nextLine
一样终止行。
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
线程“主”中的异常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 您有一个大小为10的数组A ,并且正在访问A [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 . 在您的情况下,当变量n的值为0时,您已经初始化了数组ID和Name 。要解决此错误,应在变量n初始化后初始化这些数组 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.