【问题标题】:How do I avoid StringIndexOutOfBoundsException when char has no value?当 char 没有值时,如何避免 StringIndexOutOfBoundsException?
【发布时间】:2012-10-11 20:40:26
【问题描述】:

对不起,如果标题没有意义,但我不知道如何措辞。

问题:

我正在制作一个多项选择问答游戏,从用户那里获得 a、b、c 或 d。如果他们按照他们的指示去做,这没有问题,但是如果他们不输入任何内容而只是按 Enter 键,我会得到一个 StringIndexOutOfBoundsException。我明白为什么会发生这种情况,但我是 Java 新手,想不出办法来解决它。

到目前为止我所拥有的:

    System.out.println("Enter the Answer.");

    response = input.nextLine().charAt(0);

    if(response == 'a')
    {
            System.out.println("Correct");
    }

    else if(response == 'b' || response == 'c' || response == 'd')
    {
        System.out.println("Wrong");
    }
    else
    {
        System.out.println("Invalid");
    }

当然,如果用户不键入任何内容,程序将永远不会超过第二行代码,因为您不能获取空字符串的 charAt(0) 值。我正在寻找的东西会检查响应是否为空,如果是,请返回并再次向用户提问。

提前感谢您的任何回答。

【问题讨论】:

  • 我假设input 的类型是java.util.Scanner。如果是这种情况,那么它实际上会返回一个 空字符串 而不是 null.
  • 是的,它将是空字符串。这就是为什么input.nextLine().charAt(0); 会生成 StringIndexOutOBoundsException

标签: java null char


【解决方案1】:

您可以使用 do-while 循环。直接替换

response = input.nextLine().charAt(0);

String line;

do {
  line = input.nextLine();
} while (line.length() < 1);

response = line.charAt(0);

这将在用户输入空行时继续调用input.nextLine(),但一旦他们输入非空行,它将继续并将response 设置为该非空行的第一个字符空行。如果您想重新提示用户回答,则可以将提示添加到循环内部。如果您想检查用户是否输入了字母 a-d,您还可以将该逻辑添加到循环条件中。

【讨论】:

    【解决方案2】:

    要么处理异常(StringIndexOutOfBoundsException),要么打破这条语句

        response = input.nextLine().charAt(0);
    

    作为

        String line = input.nextLine();
        if(line.length()>0){
            response = line.charAt(0);
        }
    

    异常处理:

        try{
            response = input.nextLine().charAt(0);
        }catch(StringIndexOutOfBoundsException siobe){
            System.out.println("invalid input");
        }
    

    【讨论】:

    • input.nextLine() 可以成为null 吗?
    • @A.R.S.:我的习惯是先检查空值再检查长度。已删除。
    【解决方案3】:

    简单:

    • 最初以字符串形式获取输入,并将其放入临时字符串变量中。
    • 然后检查字符串的长度。
    • 然后 if > 0 提取第一个字符并使用它。

    【讨论】:

      【解决方案4】:

      除了@HovercraftFullOfEels'(完全有效)的答案,我想指出您可以“捕捉”这些异常。例如:

      try {
          response = input.nextLine().charAt(0);
      } catch (StringIndexOutOfBoundsException e) {
          System.out.println("You didn't enter a valid input!");
          // or do anything else to hander invalid input
      }
      

      即如果在执行try-block 时遇到StringIndexOutOfBoundsException,则将执行catch-block 中的代码。您可以阅读有关捕获和处理异常的更多信息here

      【讨论】:

        【解决方案5】:

        以下情况也会出现StringIndexOutofBoundException。

        1. 搜索不可用的字符串
        2. 匹配不可用的字符串

          例如:

          列表 ans=new ArrayList();
          临时=“和”;
          字符串 arr[]={"android","jellybean","kitkat","ax"};
          for(int index=0;index if(temp.length() if(temp.equlsIgnoreCase((String)arr[``index].subSequence(0,temp.length())));
          ans.add(arr[index]);

        需要以下代码以避免 indexoutofboundexception

        if(temp.length()

        因为这里我们检查 src 字符串的长度是否等于或大于 temp 。 如果 src 字符串长度小于它会通过 "arrayindexoutof boundexception"

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-14
          • 2012-11-01
          • 2015-01-27
          • 2018-01-24
          • 2013-05-01
          • 2013-11-28
          • 1970-01-01
          • 2019-08-04
          相关资源
          最近更新 更多