开发者社区> 问答> 正文

有人可以帮助我纠正一下这个Java Pangram程序中的错误吗

我的Java代码有问题,但我无法检测到。我错了一个代码来检查pangram。Eveything看起来很麻烦,但是我的代码无法编译和运行。这是我得到的错误。线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:42 at pangram.Pangram.main(Pangram.java:29)

package pangram;

import java.util.Scanner;

public class Pangram {
    public static void main(String[] args) {
        //take input from the user
        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter a word or sentence:");
        String sentence=scan.nextLine();
        //String sentence = "the quick brown fox jumps over lazy dog";
        String sentence1=sentence.replace(" ", "");
        //extract data from the object and place in an array x
        char x[]=sentence1.toCharArray();
        int size=x.length;

        //initialize another array with zeros to fill the 26 positions of A-Z
        int y[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

        //loop through the array using while loop
        int i;
        for(i=0;i<=size;i++){
            int indexOfLetter=x[i]-65;
            //if you find the letter matching, replace 0 with 1 at that postion
            y[indexOfLetter]=1;
            ++i;
        }
        //here check if its indeed a pangram
        i=0;
        while(i!=26){ //26 is the number of letters in alphabet A-Z
            if(y[i]==1){
                i++;
            }else{
                System.out.println("NO");
            }

        }
        System.out.println("YES");

    }

}

展开
收起
垚tutu 2019-11-28 18:27:08 591 0
1 条回答
写回答
取消 提交回答
  • 精于基础,广于工具,熟于业务。

    您的程序试着运行了,输入大写的A-Z是没问题的,小写的会报错。要不是您试着把您的数据扩大一下。

    2019-11-28 19:22:00
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载