【问题标题】:Java Array index out of bounds Exception when getting string input in java在java中获取字符串输入时Java数组索引超出范围异常
【发布时间】:2012-10-20 15:24:29
【问题描述】:

在 Java 中获取字符串输入时,我得到了一个 Java ArrayIndexOutOfBoundsException。请帮我。这是我的代码:我编辑了我的代码以使用拆分:它说

线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 1 at solution2.Solution.main(Solution.java:27)

public class Solution {

 static String search;

 public static void main(String[] args){

   String[] fn,ln,tp;
  boolean[] isSet;
 Scanner sc=new Scanner(System.in);      
 int no=sc.nextInt();

 String[][] temp=new String[no][3];
 fn=new String[no];
  ln=new String[no];
   tp=new String[no];
   isSet=new boolean[no];
   boolean flag=false;

     for(int i=0;i<no;i++){
   temp[i]=sc.nextLine().split(":");
   fn[i]=temp[i][0];
   ln[i]=temp[i][1];
   tp[i]=temp[i][2];
   isSet[i]=true;

     }

       System.out.println(temp.length);

      search=sc.nextLine();

【问题讨论】:

  • 第 27 行是哪一个?您收到错误的输入是什么?
  • temp[i]=sc.nextLine().split(":"); fn[i]=温度[i][0]; ln[i]=温度[i][1]; tp[i]=温度[i][2];我们得到错误的行

标签: java string exception


【解决方案1】:

此行发生异常:

ln[i] = temp[i][1];

看起来是这样的

temp[i] = sc.nextLine().split(":");

没有在:-delimited 字符串中接收到足够的令牌来创建大小为 3 的 String 数组。

您需要确保temp[i].length == 3 以确保您可以分配这些令牌。

一个有效输入的例子(注意:没有换行符)是:

1 test:foo:bar

【讨论】:

    【解决方案2】:

    当您访问从split(":") 创建的数组上不存在的索引时,会发生 ArrayIndexOutOfBoundsException。

    在这段代码中,temp[i] 不能确保在索引 0、1、 2 处具有值,因为如果 nextLine() 是诸如“狗”之类的东西,则有没有可拆分的冒号字符。

    temp[i]=sc.nextLine().split(":");
    fn[i]=temp[i][0];
    ln[i]=temp[i][1];
    tp[i]=temp[i][2];
    

    要解决此问题,您应该在尝试访问之前验证该数组确实具有索引。

    temp[i]=sc.nextLine().split(":");
    if (temp[i].length >= 3) {
        fn[i]=temp[i][0];
        ln[i]=temp[i][1];
        tp[i]=temp[i][2];
    }
    

    【讨论】:

      【解决方案3】:

      我插入 sc.nextLine()。低于 int no = sc.nextInt();行。

      导入 java.util.Scanner;

      公共类解决方案{

      static String search;
      
      public static void main(String[] args) {
      
          String[] fn, ln, tp;
          boolean[] isSet;
          Scanner sc = new Scanner(System.in);
          int no = sc.nextInt();
          sc.nextLine(); // **********
      
          String[][] temp = new String[no][3];
          fn = new String[no];
          ln = new String[no];
          tp = new String[no];
          isSet = new boolean[no];
          boolean flag = false;
      
          for (int i = 0; i < no; i++) {
              temp[i] = sc.nextLine().split(":");
              fn[i] = temp[i][0];
              ln[i] = temp[i][1];
              tp[i] = temp[i][2];
              isSet[i] = true;
      
          }
      
          System.out.println(temp.length);
      
          search = sc.nextLine();
      }
      

      }

      【讨论】:

        【解决方案4】:

        您的问题是,按 [Enter] 后 sc.nextLine() 返回新行字符串(例如“\n”)。第二次是等你输入。见Java String Scanner input does not wait for info, moves directly to next statement. How to wait for info?

        在您的情况下,尝试在处理之前调用 sc.nextLine():

        sc.nextLine()
        temp[i]=sc.nextLine().split(":");
        

        编辑:你是对的,你必须在 nextInt() 之后插入它,因为 nextLine() 会消耗完整的行。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-12
          • 2012-11-01
          • 1970-01-01
          • 2018-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多