在JAVA中遇到ArrayIndexOutOfBoundsException异常需要帮助。

huangapple go评论67阅读模式
英文:

Need help in JAVA with ArrayIndexOutOfBoundsExceptions

问题

以下是翻译好的内容:

所以我现在正在上Java课程,我们应该编写一个使用ArrayIndexOutOfBoundsException的程序。很简单,对吧?但出于某种原因,我得到了以下错误:

"错误:不兼容的类型:无法将ArrayIndexOutOfBoundsException转换为Throwable"

import java.util.*; //用于异常处理

class ArrayIndexOutOfBoundsException {
public static void main(String[] args) {
int[] array = fillArray(); //填充数组,包含100个值
int userInput = getInput();
try {
  System.out.print("元素 " + userInput + " 的值是 " + array[userInput]);
}
catch (ArrayIndexOutOfBoundsException ex) {
  System.out.print("超出边界");
}
}//结束main

public static int[] fillArray(){ //填充数组的函数
int[] array = new int[100];
for (int i = 0; i < 100; i++) {
  array[i] = (int)(Math.random() * 100) + 1;
}
return array;
}//结束fillArray

public static int getInput(){ //确保用户输入正确值的函数
System.out.print("请输入要查找相应值的索引:");
Scanner input = new Scanner(System.in); //用于输入
int userInput = 0;
try {
  userInput = input.nextInt();
}
catch (InputMismatchException ex) {
  System.out.println("\n不是有效的数字\n请重试"); 
  return getInput();
}
return userInput;
} //结束getInput

}//结束类

这是我运行的代码"catch (ArrayIndexOutOfBoundsException ex) {" 处发生错误

然而如果我将其更改为IndexOutOfBoundsException代码将按预期工作我真的找不出为什么我的意思是它应用于数组所以ArrayIndexOutOfBoundsException不适用吗至少根据我尝试在Google中搜索问题所得到的信息是这样的...但显然我还遗漏了其他内容

我在Repl.it上运行它我不认为这会有什么影响但谁知道呢

另外对于格式不佳的代码我道歉由于某种原因当我复制粘贴它时格式会变得混乱所以这是我能够在合理的时间内修复以使其可读的最佳方式

无论如何任何帮助/建议/甚至是指引我朝着正确方向的提示都将不胜感激谢谢

<details>
<summary>英文:</summary>

so I&#39;m in a Java class right now and we&#39;re supposed to write a program using ArrayIndexOutOfBoundsExceptions. Simple enough, right? Well for whatever reason I&#39;m getting the error:

&quot;error: incompatible types: ArrayIndexOutOfBoundsException cannot be converted to Throwable&quot;


    import java.util.*; //for exceptions
    
    class ArrayIndexOutOfBoundsException {
    public static void main(String[]args){
    int[] array = fillArray(); //filling the array with 100 values
    int userInput = getInput();
    try{
      System.out.print(&quot;The value of element &quot;+userInput+&quot; is &quot;+array[userInput]);
    }
    catch (ArrayIndexOutOfBoundsException ex) {
      System.out.print(&quot;Out of Bounds&quot;);
    }
    }//end main
    
    public static int[] fillArray(){ //function to fill the array
    int[] array = new int[100];
    for (int i = 0; i&lt;100;i++)
    {
      array[i] = (int)(Math.random() *100) + 1;
    }
    return array;
    }//end fillArray
    
    public static int getInput(){ //function to make sure the user enters a proper value
    System.out.print(&quot;Please enter the index value to find the corresponding value at that location: &quot;);
    Scanner input = new Scanner(System.in); //for input
    int userInput = 0;
    try{
      userInput = input.nextInt();
    }
    catch(InputMismatchException ex){
      System.out.println(&quot;\nNot a valid number\nPlease Try Again&quot;); 
      return getInput();
    }
    return userInput;
    } //end getInput
    
    }//end class

This is the code I&#39;m running, with the error occurring at the &quot;catch (ArrayIndexOutOfBoundsException ex) {&quot;. 

However, if I switch that to just an IndexOutOfBoundsException the code works as intended and I cannot for the life of my figure out why. I mean, it&#39;s being applied to an Array so shouldn&#39;t an ArrayIndexOutOfBoundsException apply? At least that&#39;s what I&#39;ve been able to find trying to google the problem...but clearly there is something else I&#39;m missing.

I&#39;m running it on Repl.it, I don&#39;t think that would make a difference, but never know?

Also, I apologize for the poorly formatted code, for whatever reason when I copy and paste it, it get&#39;s all messed up so this was the best I was able to fix it to make it readable in a reasonable amount of time.

Anyways, any help/advice/so much as pointing me in the right direction would be appreciated! Thank you!

</details>


# 答案1
**得分**: 1

更改你的类名它就会起作用这是最佳解决方案因为它可以阻止对java.lang.ArrayIndexOutOfBoundsException的掩盖

```java
class ArrayIndexOutOfBoundsExceptionTest {
  public static void main(String[] args) {
    int[] array = fillArray(); // 填充数组,包含100个值
    int userInput = getInput();
    try {
      System.out.print("元素 " + userInput + " 的值是 " + array[userInput]);
    } catch (ArrayIndexOutOfBoundsException ex) {
      System.out.print("越界");
    }
  } // 结束 main

为了完整和演示的目的,在这里我没有重新命名类,而是使用了异常的完整路径名(java.lang):

class ArrayIndexOutOfBoundsException {
  public static void main(String[] args) {
    int[] array = fillArray(); // 填充数组,包含100个值
    int userInput = getInput();
    try {
      System.out.print("元素 " + userInput + " 的值是 " + array[userInput]);
    } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
      System.out.print("越界");
    }
  } // 结束 main
英文:

Change the name of your class and it will work. This is the best solution, as it stops the masking of java.lang.ArrayIndexOutOfBoundsException:

class ArrayIndexOutOfBoundsExceptionTest {
public static void main(String[]args){
int[] array = fillArray(); //filling the array with 100 values
int userInput = getInput();
try{
System.out.print(&quot;The value of element &quot;+userInput+&quot; is &quot;+array[userInput]);
}
catch (ArrayIndexOutOfBoundsException ex) {
System.out.print(&quot;Out of Bounds&quot;);
}
}//end main

For completeness and demonstration purposes, here I haven't renamed the class, but instead used the full pathname (java.lang) for the Exception

class ArrayIndexOutOfBoundsException { 
public static void main(String[]args){
int[] array = fillArray(); //filling the array with 100 values
int userInput = getInput();
try{
System.out.print(&quot;The value of element &quot;+userInput+&quot; is &quot;+array[userInput]);
}
catch (java.lang.ArrayIndexOutOfBoundsException ex) {
System.out.print(&quot;Out of Bounds&quot;);
}
}//end main

答案2

得分: -1

你的实现存在几个问题。

  1. 已经存在一个名为 ArrayIndexOutOfBoundsException 的类。
  2. 你会在文档中看到该类的层次结构包括 Throwable,而你的类没有包含这一点。

在JAVA中遇到ArrayIndexOutOfBoundsException异常需要帮助。

因此,你的类需要扩展 IndexOutOfBoundsException 或 Throwable 类。

英文:

There are a couple of problems with your implementation.

  1. There is already a class called ArrayIndexOutOfBoundsException.
  2. You will see in the documentation that the class's hierarchy includes Throwable, which your class does not.

在JAVA中遇到ArrayIndexOutOfBoundsException异常需要帮助。

So you need your class to either extend the IndexOutOfBoundsException or the Throwable class.

huangapple
  • 本文由 发表于 2020年9月24日 21:08:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/64047168.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定