有人可以帮助我纠正Java Pangram程序中的此错误

时间:2019-11-28 09:35:03

标签: java

我的Java代码有问题,但我无法检测到。我错了一个代码来检查pangram。 Eveything看起来很麻烦,但是我的代码无法编译和运行。这是我得到的错误。 线程“主”中的异常java.lang.ArrayIndexOutOfBoundsException:42     在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");

    }

}

5 个答案:

答案 0 :(得分:1)

第一,我必须小于大小,第二,不要在for循环中递增i,它在每次迭代后都会递增。

一种可能的解决方案

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a word or sentence:");
String sentence = scan.nextLine();
String sentence1 = sentence.toLowerCase().replace(" ", "");

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
for(int i=0; i < size; i++){
    int indexOfLetter = x[i] - 'a';
    //if you find the letter matching, replace 0 with 1 at that postion
    y[indexOfLetter]=1;
}
//here check if its indeed a pangram
boolean pangram = true;
for (int i = 0; i < 26 && pangram; i++) {
    if (y[i] == 0) {
       pangram = false
    }
}

System.out.println(pangram ? "YES" : "NO");

答案 1 :(得分:0)

问题1: 第22行

   int indexOfLetter=x[i]-65;

似乎不正确,您在输入字符串中得到第一个字母,这仅在使用Integer表示形式时才适用于大写单词。 B和b具有不同的整数表示形式,因此任何小写字母-65都将大于26。要解决此问题,请在String sentence=scan.nextLine()之后添加.toUpperCase()

问题2:第25行

       ++i;

此操作完成两次,一次进入for循环

for(i=0;i<=size;i++)

请删除++ i;

问题3:第21行

for(i=0;i<=size;i++)

在遍历数组时,应在i < size而不是i <= size处结束,这是因为数组从0开始,所以大小为4的值为0、1、2、3。运行此命令并拥有i <= size时,您实际上尝试对输入数组中的4个元素运行for循环5次。

问题4:您的while循环最后会导致无限循环。 相反,只需检查数组是否包含0值,如果是,则打印“否”,而不打印“是”

        boolean foundZero = true;

    for (int a = 0; a < y.length; a++){
        if(y[a] == 0){
            foundZero = false;
        }
    }

    System.out.println(foundZero);

尽管此打印为true false 希望这可以帮助!

答案 2 :(得分:0)

确切地说,您打算在第一个循环中做什么?

答案 3 :(得分:0)

you can do this way

package myproject.lambda;

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.toUpperCase().toCharArray();
        // byte x[]=sentence1.toUpperCase().getBytes();
        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;
        }
        // here check if its indeed a pangram
        i = 0;
        boolean isPangram = true;

        for (int num : y) {
            if (num == 0) {
                isPangram = false;
                break;
            }
        }

        if (isPangram) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }

    }

}

答案 4 :(得分:0)

Little optimize in java 8

package myproject.lambda;

import java.util.Arrays;
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.toUpperCase().toCharArray();
        // byte x[]=sentence1.toUpperCase().getBytes();
        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;
        }
        // here check if its indeed a pangram

        if (Arrays.stream(y).anyMatch(k -> k == 0)) {
            System.out.println("NO");
        } else {
            System.out.println("YES");
        }

    }

}