-3

I am supposed to read from a text file and figure if a sentence is a PANGRAM (uses all characters in the alphabet), a PERFECT pangram (uses every character in the alphabet once), or NEITHER. The guide says that I am supposed to initialize x with '0', then go through each letter in the sentence, and if the letter matches with a character in the alphabet, I increment x by 1; then at the end, if the value x for each sentence has a '0', it's NEITHER, if it's all '1', it's PERFECT ,if there is no '0', it's PANGRAM.

Scanner pangram = new Scanner ( 
PerfectPangram.class.getResourceAsStream ( "pangram.txt" ));
    while (pangram.hasNext())
    {
        String text = pangram.nextLine().toUpperCase();

        char [] alphabet = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};

        char txt[] = text.toCharArray();
        int zero[] = new int [text.length()];
        for(int i = 0; i < zero.length; i++)
        {
            zero[i] = 0;
        for (int a = 0; a < text.length(); a++) 
        {   
            zero[i] = txt[a];
        for (int array = 0; array < alphabet.length; array++) 
        {           
        if (alphabet[array] == text.charAt(i)) 
        {
            zero[i]++;              
        }
        }
        }
        if (zero[i] == 1) 
        {   
        }
        }   
    }

Output should be like this:

PANGRAM: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.

NEITHER: ALL YOUR BASE ARE BELONG TO US; SOMEONE SET US UP THE BOMB. PERFECT: NEW JOB: FIX MR GLUCK'S HAZY TV, PDQ.

An Dang
  • 1
  • 1
  • Okay... are you stuck on any particular aspect of your homework assignment? – phatfingers Feb 20 '18 at 04:03
  • You need to revisit the lesson on how curly braces (`{}`) work, especially in connection with `for` statements. You've got a matching `}` for each `{`, but as far as I can tell almost none of the `}`s are in the right places. – Kevin Anderson Feb 20 '18 at 04:27

1 Answers1

0

You got it all wrong I guess. First of all your counter array (zero) is initialized with wrong length. Its fixed length should be 'Z'(90) - 'A'(65) + 1 = 26 as is the count of the letters in the latin alphabet. Then you should increment the index of that counter array representing the characters position in the alphabet (c - 'A'), but only if the character is part of the alphabet (c >= 'A' && c <= 'Z'). Once you do this for each of the symbols in a sentence you should now iterate throughout the members of zero and find if any of them is lower then 1: not a pangram; or any is higher then 1: not a perfect pangram;

I will post some crappy code of mine just to demonstrate it but I will not advise you to go with it as your final homework ;) I hope you get the basics and you are able to do it on your own now.

public static void main(String[] args) {

    String test = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.\nALL YOUR BASE ARE BELONG TO US; SOMEONE SET US UP THE BOMB.\nNEW JOB: FIX MR GLUCK'S HAZY TV, PDQ.";

    Scanner pangram = new Scanner (test);

    int zero[] = new int ['Z' - 'A' + 1];

    while (pangram.hasNextLine())
    {
        Arrays.fill(zero, 0);

        String text = pangram.nextLine().toUpperCase();

        for (char c : text.toCharArray())
        {
            if (c >= 'A' && c <= 'Z') {
                zero[c - 'A']++;
            }
        }

        boolean isPangram = true;
        boolean isPerfectPangram = true;

        for (int i : zero) {
            if (i < 0) {
                isPangram = false;
                break;
            }
            if (i > 1) {
                isPerfectPangram = false;
            }
        }

        System.out.println(isPerfectPangram ? "Prefect Pangram" : isPangram ? "Pangram" : "Neither");
    }
}
dbl
  • 1,109
  • 9
  • 17