0

i have a problem in my java code but i am unable to detect. I wrong a code to check for pangram. Eveything looks fone but my code will not compile and run. here is the error i get. Exception in thread "main" 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");

    }

}
George
  • 1
  • 1

4 Answers4

1

First, i must be less than size, second don't increment i in for loop, it's incremented after each iteration.

One possible solution

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");
b.GHILAS
  • 2,273
  • 1
  • 8
  • 16
0

Problem 1 : line number 22

   int indexOfLetter=x[i]-65;

seems to be incorrect, you get the first letter in the input string, this only works for upper case words as you are using the Integer representation. B and b have different integer representations so anything lower case -65 will be greater than 26. To fix this, after the String sentence=scan.nextLine() add .toUpperCase()

problem 2: line 25

       ++i;

This is done twice, once inside the for loop

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

Please remove the ++i;

Problem 3: line 21

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

When looping through the array you should end when i < size and not when i <= size This is because arrays start at 0 so something of size 4 has values 0,1,2,3. When you run this and have i <= size you actually try and run the for loop 5 times for 4 elements in the input array.

problem 4: you're while loop at the end causes an infinite loop. instead simply check if the array contains a 0 value and if so, print 'no', it not print 'yes'

        boolean foundZero = true;

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

    System.out.println(foundZero);

Although this prints true false Hope this helps!

Pete
  • 205
  • 2
  • 14
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");
        }

    }

}
vivekdubey
  • 484
  • 2
  • 7
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");
        }

    }

}
vivekdubey
  • 484
  • 2
  • 7