【问题标题】:i keep getting an array out of bound exception [duplicate]我不断让数组超出界限异常[重复]
【发布时间】:2017-09-15 20:30:27
【问题描述】:

我正在尝试制作一个拆分字符串的程序,并返回具有 3 个或更多元音的字符串,并且我不断得到一个数组越界异常。我不知道如何在方法结束时返回一个数组。

我显然看不到的程序中的问题在哪里?

public class SplitWords {

    public static void main(String [] args){
        Scanner scan =  new Scanner(System.in);

        final int MIN_SIZE = 4;
        int size = 0;

        do{

            System.out.println("How many words are you typing(min " + MIN_SIZE + "): ");
            size = scan.nextInt();
        }while(size < MIN_SIZE);
        scan.nextLine();

        String[] myarray = new String[size];
        for(int i = 0; i < myarray.length; i++){
            System.out.println("Type the sentence in the position " + i);
            myarray[i] = scan.nextLine();
        }

        System.out.println("The words which have 3 or more vowels are: " + findVowels(myarray));
    }

    public static String findVowels(String[] myarray){
    for(int i = 0; i < myarray.length; i++){
        String[] tokens = myarray[i].split(" ");
                int count = 0;
                for(int j = 0; j < myarray.length; j++) {
                    if(isVowel(tokens[i].charAt(j))){
                           count++;                     
                    }
                 }
                 if(count > 3){
                  break;
                 }
    }
        return null;
    }

    public static boolean isVowel(char ch){
            switch(ch){
                case 'a':
                    case'e':
                        case'i':
                            case'o':
                                case'u':
                                    case'y':
                                        return true;
            }
            return false;
    }
} 

【问题讨论】:

  • tokens[i].... 为什么你会认为tokens 至少有i+1 元素?
  • if(isVowel(tokens[i].charAt(j))) 中是什么让你认为tokens[i] 确实存在?

标签: java arrays string split do-while


【解决方案1】:

为什么要将字符串拆分为标记?也调用 String[],我猜你的意思是 char[] 或 String,因为你正在做的是创建一个字符串数组,并且就像字符串数组是单个字符串一样。只需使用“字符串”而不是“字符串 []”

【讨论】:

  • 非常抱歉各位,我是大学一年级的初学者,这是一个作业,它会在我的考试中帮助我
【解决方案2】:

这就是问题

 for(int j = 0; j < myarray.length; j++) {
                    if(isVowel(tokens[i].charAt(j))){
                           count++;                     
                    }
                 }

当你拆分字符串时

 String[] tokens = myarray[i].split(" ");

你为什么不使用tokens.length而不是myarray.length,使用tokens[j]而不是i,i是你拥有的字符串数量的计数器。

整合上述更改后,您的代码应如下所示

public static String findVowels(String[] myarray){

        for(int i = 0; i < myarray.length; i++){

            String[] tokens = myarray[i].split(" ");
                    int count = 0;
                    for(int j = 0; j < tokens.length; j++) {

                        String str = tokens[j];

                        for (int j2 = 0; j2 < str.length(); j2++) {

                            if(isVowel(str.charAt(j2))){
                                   count++;                     
                            }
                             if(count > 3){
                              break;
                             }
                        }

                     }

        }
            return null;
        }

这并没有给出任何异常,但我对这段代码的逻辑感到非常惊讶,因为无论如何你都会在方法结束时返回 null。

【讨论】:

  • 我按照你说的做了,结果返回 null
  • 您的代码包含很多错误,我已经修复了导致问题的方法,tokens[j] 是一个字符串,您需要迭代该字符串以检查每个字符,您在您的代码中做错了代码
  • 我不知道如何返回带有拆分单词的数组,这就是问题
猜你喜欢
  • 2015-06-28
  • 1970-01-01
  • 2013-11-28
  • 2015-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多