将字符数组与 KeyEvent 进行比较时获取 ArrayIndexOutOfBoundsException

问题描述

我正在构建一个 JavaFX 输入应用程序,并试图检查用户输入是否与他们需要输入的字母匹配。我是通过 KeyEvent 执行此操作并将其与数组中的匹配索引单独进行比较。这样做后,我得到了很多错误,主要原因是

Caused by: java.lang.Arrayindexoutofboundsexception: Index 117 out of bounds for length 5 at sample.Controller.detectInputAreaKeyEvent(Controller.java:51)

控制器

    @FXML
    private void detectInputAreaKeyEvent(KeyEvent event) {
        char[] splitter = textToType.tochararray();
        for (char c : splitter) {
            if (event.getText().equals(splitter[c])) {
                System.out.println("right letter");
            }
            else {
                System.out.println("wrong letter");
            }
        }

    }

FXML

      <StackPane fx:id="typingArea" prefheight="150.0" prefWidth="200.0" borderpane.alignment="CENTER">
         <children>
            <Text fx:id="sixsixsix" strokeType="OUTSIDE" strokeWidth="0.0" text="urmom">
               <font>
                  <Font size="40.0" />
               </font>
            </Text>
            <TextArea fx:id="inputArea" onKeypressed="#detectInputAreaKeyEvent" opacity="0.29" prefheight="200.0" prefWidth="200.0" />
         </children>
      </StackPane>

主类

String textToType = "test";

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)