当前位置: 首页 > 知识库问答 >
问题:

数组*未抛出越界异常

祝允晨
2023-03-14

我正在尝试学习异常处理。我似乎找不到String[]a=names(scnr);若要在超出3个元素时抛出超出界限的异常,请执行以下操作。我知道,大多数人都讨厌越界的错误,而我正在努力让它发生,我一辈子都搞不清我到底做错了什么。一整天都在搜索各种东西。但我似乎找不到我要找的东西。所以我需要一些帮助和视角。

因此,我输入一个完整的字符串,我根据逗号和空格对其进行定界(修剪和拆分),然后将这些片段存储到一个数组中(string[]name),然后传递给main,用string[]a输出。所以当我超越3个元素时,不管我怎么做,这都不是错误的。我只能显示除了A之外的任何东西。但那不是我真正想做的。这是我的第一个java类,所以要温柔点,哈哈。

有什么建议吗?

 import java.util.*;

public class ReturnArrayExample1  
{  
    public static void main(String args[])
    {  
        Scanner scnr = new Scanner(System.in);
        String[] a = names(scnr);           
        for (int i = 0; i < 3; i++) 
        {
            System.out.println(a[i] + " in index[" + i + "].");
        }
        scnr.close();
    }  

    public static String[] names(Scanner scnr)  
    {                           
        String[] name = new String[3];      // initializing
        boolean run = true;
    do 
    {
        try
        {
        System.out.println("Enter 3 names separated by commas ',':(Example: keith, mark, mike)");
        String rawData = scnr.nextLine();
            if(rawData.isEmpty())
            {
                System.err.println("Nothing was entered!");
                throw new Exception();
            }
            else
            {
                name = rawData.trim().split("[\\s,]+");
                run = false;
            }
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.err.println("Input is out of bounds!\nUnsuccessful!");
        }
        catch(Exception e)
        {
            System.err.println("Invalid entry!\nUnsuccessful!");
        }
    }
    while(run == true);
    
    System.out.println("Successful!");
    scnr.close();
    return name;  
    }  
}

共有1个答案

匡安宜
2023-03-14

如果我没有理解您的意思,那么如果names数组不正好包含3个元素,您希望抛出一个ArrayOutOfBoundsException。下面的代码与您使用if语句编写的代码相同。

import java.util.*;

public class ReturnArrayExample1  
{  
   public static void main(String args[])
   {  
       Scanner scnr = new Scanner(System.in);
       String[] a = names(scnr);           
       for (int i = 0; i < 3; i++) 
       {
           System.out.println(a[i] + " in index[" + i + "].");
       }
       scnr.close();
   }  

   public static String[] names(Scanner scnr)  
   {                           
       String[] name = new String[3];      // initializing
       boolean run = true;
   do 
   {
       try
       {
       System.out.println("Enter 3 names separated by commas ',':(Example: keith, mark, mike)");
       String rawData = scnr.nextLine();
           if(rawData.isEmpty())
           {
               System.err.println("Nothing was entered!");
               throw new Exception();
           }
           else
           {
               name = rawData.trim().split("[\\s,]+");
               if (name.length != 3) {
                   throw new ArrayIndexOutOfBoundsException();
               }
               run = false;
           }
       }
       catch(ArrayIndexOutOfBoundsException e)
       {
           System.err.println("Input is out of bounds!\nUnsuccessful!");
       }
       catch(Exception e)
       {
           System.err.println("Invalid entry!\nUnsuccessful!");
       }
   }
   while(run == true);
   
   System.out.println("Successful!");
   scnr.close();
   return name;  
   }  
}
 类似资料:
  • 这是我的代码: 该程序的目的是要求用户输入一个字符串,然后统计字符串中每个字符的使用次数。 当我去编译程序时,它工作正常。当我运行程序时,我可以在弹出框中输入字符串,但是在我提交字符串并按确定后,我得到一个错误,说 我不完全确定问题是什么或如何解决。

  • 我一直在四处寻找是否有任何东西可以帮助我,但我不太了解人们的回答,任何我所了解的似乎都不能解决问题!所以基本上正如标题所说,我得到了一个数组索引越界异常,但我不知道为什么。非常感谢任何帮助。 代码:

  • 我很困惑为什么会发生这种情况。我已经在这方面工作了一段时间,我只是不明白。 我的Map代码工作时,我能够在它所在的目录中验证输出。 文件中的输出如下所示: 但是为什么我不能拆分

  • 问题内容: 在我的游戏代码中,我尝试添加一张手牌。一旦我做完了,我的数组就超出了范围。一切看起来都不错,但也许我缺少了一些东西。 仅供参考,一个和两个是Player实例。来自Main类的相关代码(对格式感到抱歉。我很想将其传输到Stack Overflow): 卡类: 玩家等级: 问题答案: 问题出在你的循环上 没有其他任何值可设置,因此此循环不断循环,直到所有玩家拥有超过52张牌为止。一旦某人拥

  • 我使用String[3]数组来存储来自4个JTextFields的用户输入,然后将该数组输出到一个txt文件: 有人能告诉我为什么我会得到异常“数组索引越界”吗?谢谢。

  • 我正在为一个老式电话计划编写Java代码,所以我有: 频段类: 速率类: 我想在Rate类中编写一个方法,如果给定一周中的某一天,它将返回一个由该周中某一天的带组成的带数组。 我写的是: 但是我一直得到一个索引越界异常(索引2越界,长度为2)。 我怎么能解决这个?