【问题标题】:Java: Program says out of index but everything in code is correctJava:程序说索引不足,但代码中的所有内容都是正确的
【发布时间】:2021-12-18 23:34:20
【问题描述】:

几天前我必须完成我的学校作业,我想出了这个简单的程序,它根据用户输入的内容返回 truefalse 值.如果单词以字符 'a' 或 'e' 结尾,程序应该返回 true 值,否则返回 false.. 我们严格需要使用循环和 NOT USE endsWith 或任何其他类来创建我们自己的方法。所以我的想法基本上是将单词拆分为字符并用它们填充一个表,然后最后我使用 if 语句检查保存在表索引中的字符是否匹配 'a ' 或 'e'。我对其他解决方案不感兴趣,我只想解释为什么程序总是返回下面列出的错误。

附言我不是任何编程语言的高级程序员,所以不要评判我。

错误

java.lang.ArrayIndexOutOfBoundsException: 0
    at Homework.myMethod(Homework.java:34)
    at Homework.main(Homework.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)enter code here
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)  

计划:

import java.io.*;
public class Homework
{
  public static void main(String[] args) throws IOException
  {
    //Defined reader
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    
    //Instruction for user
    System.out.println("Please enter any word you like:");
    
    //Reading user input
    String a = in.readLine();
    
    //Showing user output
    if(myMethod(a) == true){
      System.out.println("Word has char 'a' or 'e' at the end of the word.");
    }
    else{
      System.out.println("Word doesn't have char 'a' or 'e' at the end of the word.");
    }
  }
  
  //My defined method
  public static boolean myMethod(String str)
  {
    //Empty table
    char table[]={};
    
    //Loop
    for(int i = 0; i < str.length(); i++)
    {
      //Grabs first char of word and adds it to the table
      table[i] = str.charAt(i);
    }
    
    //Check statement to see if there is actually char 'a' or 'e' at the end of the word.
    if(table[table.length-1] == 'a' || table[table.length-1] == 'e')
    {
      return true;
    }
    return false;
  }
}

【问题讨论】:

  • char table[]={}; 您声明了一个大小为零的数组。代码绝对正确:)。数组大小不会因为你添加东西而改变。请改用ArrayList

标签: java arrays sorting indexing


【解决方案1】:

问题是char table[] = {}是一个空数组,那么你不能在里面赋值,你可能需要创建一个大小

char[] table = new char[str.length()];

有一个更好的方法是str.toCharArray()。此外,当您根据条件返回true/false 时,只需返回条件

public static boolean myMethod(String str) {
    char[] table = str.toCharArray();
    return table[table.length - 1] == 'a' || table[table.length - 1] == 'e';
}

另外Scanner更容易使用,if (myMethod(a))作为条件就足够了

Scanner in = new Scanner(System.in);
System.out.println("Please enter any word you like:");
String a = in.nextLine();
if (myMethod(a)) {
    System.out.println("Word has char 'a' or 'e' at the end of the word.");
} else {
    System.out.println("Word doesn't have char 'a' or 'e' at the end of the word.");
}

【讨论】:

    【解决方案2】:

    数组的大小是固定的。您的数组的长度基本上为 0。尝试类似

    char table[]= new char[str.length()];
    

    【讨论】:

      【解决方案3】:

      如果你必须使用 for 循环,你可以使用它,它本质上(现在我看)与 azro 的方法相同,只是 toCharArray() 方法被显式写为循环......

      if (str == null || str.length() < 1) return false;
      
      char[] chars = new char[str.length()];
      for (int i = 0; i < chars.length; i++) chars[i] = str.charAt(i);
      return (chars[chars.length - 1] == 'a' || chars[chars.length - 1] == 'e');
      

      【讨论】:

        猜你喜欢
        • 2020-12-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多