StringIndexOutOfBoundsException

前端 未结 4 629
花落未央
花落未央 2021-01-29 09:03

I am having a few problems in getting a method to work properly in Java. In the program I have created an array consisting of many different words of different length. The met

4条回答
  •  野的像风
    2021-01-29 09:05

    If you have replaced

    for (int index = 0; index < wordTable.length; index++) {
    

    with

    for (int index = 0; index < length; index++) {
    

    that is most likely not what you really want.

    The better approach would be like this:

    public static void inputMethod(int length, char letter, int position) {
      // check if input is valid
      if(position < length)
        for (int index = 0; index < wordTable.length; index++) {
          if (length == wordTable[index].length() && letter == wordTable[index].charAt(position) )
                System.out.println(wordTable[index]);
    }
    

提交回复
热议问题