【问题标题】:Array Index Out Of Bounds Exception using Integer.parseInt使用 Integer.parseInt 的数组索引越界异常
【发布时间】:2013-04-24 23:40:54
【问题描述】:

我的程序符合要求,但是当我运行该程序时,它给了我一个“数组索引越界异常”

public void readBoard(String filename) throws Exception { 
    File f = new File("myBoard.csv"); 
    Scanner reader = new Scanner(f); 
    while(reader.hasNext()){ 
        String line = reader.nextLine(); 
        String [] b = line.split(","); 
        String type = b[0]; 
        int i = Integer.parseInt(b[1]); 
        int j = Integer.parseInt(b[2]); 
        if(type.equals("Chute")) 
            board[i][j] = new Chute(); 
        else if(type.equals("Ladder")) 
            board[i][j] = new Ladder(); 
}

错误在 int i = Integer.parseInt(b[1]);我的问题是我将 String [1] 和 [2] 转换为 int 的方式正确吗?我认为这不是因为我有一个数组越界异常。我猜这意味着它指向该区域的某个位置并且什么都没有。

【问题讨论】:

    标签: arrays file exception parseint


    【解决方案1】:

    IndexOutOfBounds 确实意味着您正在尝试访问数组中不存在的元素。添加:

    System.out.println("array size = " + b.length);
    

    查看数组的实际长度。您希望您的数组的长度为 3,但根据实际读取的行和拆分它的方式,您似乎有一个长度为 1 的数组。这也有助于查看您的实际行试图分裂。

    试试这个:

    public void readBoard(String filename) throws Exception{
        File f = new File("myBoard.csv");
        Scanner reader = new Scanner(f);
        while(reader.hasNext()){
            String line = reader.nextLine();
    
            // What line do we intend to process?
    
            System.out.println("Line = " + line);
    
            String [] b = line.split(",");
    
            // How long is the array?
    
            System.out.println("Array length = " + b.length);
    
            String type = b[0];
            int i = Integer.parseInt(b[1]);
            int j = Integer.parseInt(b[2]);
            if(type.equals("Chute"))
                board[i][j] = new Chute();
            else if(type.equals("Ladder"))
                board[i][j] = new Ladder();
        }
    

    无论何时在开发代码的过程中,您都希望添加调试语句来转储各个字段的值,以帮助您了解自己在做什么。在您的代码中添加一些关键的调试语句将帮助您协调您的假设(即“我的数组包含三个元素”)与实际发生的事情(即“我的数组只有一个元素”)。

    【讨论】:

    • 哦,这有帮助!谢谢你。打印出数组的大小很有帮助。它证明数组的大小是 1 而不是 3。现在我知道问题出在哪里,在文件中方法正在调用。非常感谢!
    • 很高兴为您服务。 Sprinkling 很有趣,可以帮助您的代码增长。这是最基本的调试技术,只要您继续编码,它就会为您服务。只要记住在你的发布版本中删除调试语句,或者你总是可以包装调试语句。在你的类中创建一个名为 DEBUG 的最终静态布尔变量,然后当你需要洒水时,只需使用 if(DEBUG) System.out.println("myVar" + myVar);
    【解决方案2】:

    确保行拆分工作正常,并且您有 3 个不同的字符串 o.w。 b[1] 或 b[2] 应该会导致错误。打印或调试,看看 b[0] 的值是多少。

    【讨论】:

      【解决方案3】:

      试试这个,因为数组大小为 1 超出范围,你应该跳过所有大小为 1 的数组。

      public void readBoard(String filename) throws Exception {
          File in = new File("myBoard.csv");
          Scanner reader = new Scanner(in);
          while (reader.hasNext()) {
              String line = reader.nextLine();
              String[] b = line.split(",");
              if (b.length != 1) {
                  String type = b[0];
                  int i = Integer.parseInt(b[1]);
                  int j = Integer.parseInt(b[2]);
                  if (type.equals("Chute"))
                      board[i][j] = new Chute();
                  else if (type.equals("Ladder"))
                      board[i][j] = new Ladder();
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        在 while 循环之前执行此操作

        reader.nextLine[];
        

        因为文件的第一行只有一个元素。

        【讨论】:

          猜你喜欢
          • 2013-03-01
          • 2016-08-03
          • 2016-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-25
          相关资源
          最近更新 更多