The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Pangram Question (All the letters of the alphabet in a sentence)

I've searched through the forum to see if this question has been posted previously but I can't find it so apologies in advance if I missed it...

What I am trying to do is to run the program to check through each letter of the sentence and as it does this I want it to record if it finds a letter through it. At the end of the loop, I want it to tell me if it found all the letters of the alphabet (a pangram) or if it did not. however I cannot get it to achieve this.

Can someone give me some guidance?

importjavax.swing.JOptionPane;publicclass CSQ2
{publicstaticvoid main (String[] agrs){String sentence =JOptionPane.showInputDialog(null, "Please enter a sentence");String result ="";String 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";if(sentence.equals(""))
result ="You have not entered a sentence";elseif(sentence.length()<26)
result ="You have not entered a pangram";elseif(sentence.length()>=26)for(int i =0; i<sentence.length();i++){String aChar = sentence.substring(i,i+1);String alph = alphabet.substring(i,i+1);if(alph.indexOf(aChar)!=-1)
result ="You have entered a pangram";else
result ="You have entered a pangram";}JOptionPane.showMessageDialog(null, result);}}

Re: Pangram Question (All the letters of the alphabet in a sentence)

Firstly, never ever code if elses without braces, even if they only contain one line of code. Secondly think ASCII. convert the entered sentence to lower case and use nested loops to iterate over the letters of the alphabet and the sentence itself.

Re: Pangram Question (All the letters of the alphabet in a sentence)

Use an array of 26 elements to count the frequency of the characters found in the sentence using PhHein's suggestion of considering the characters as char(s) which are numbers. String methods will help with this. Read the String API to find the methods that will be most helpful. Any of the 26 elements with frequency of zero indicate that the sentence is not a pangram.

Re: Pangram Question (All the letters of the alphabet in a sentence)

okay, made the changes regarding the lower case and the braces for the else if statement.

Not sure I follow you on the suggestion of the arrays. Are you saying to convert all the characters in the string alphabet to ASCII?

importjavax.swing.JOptionPane;publicclass CS5051Q2
{publicstaticvoid main (String[] agrs){String sentence =JOptionPane.showInputDialog(null, "Please enter a sentence");String sentenceCopy = sentence;
sentence = sentence.toLowerCase();String result ="";String 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";if(sentence.equals(""))
result ="You have not entered a sentence";else{if(sentence.length()<26)
result ="You have not entered a pangram";}else{if(sentence.length()>=26)for(int i =0; i<sentence.length();i++){String alph = alphabet.substring(i,i+1);if(sentence.indexOf(alph)!=-1)
result ="You have entered a pangram";else
result ="You have entered not entered a pangram";}}JOptionPane.showMessageDialog(null, result);}}

--- Update ---

Okay, sorry the program seems to run now, have I avoided using arrays or is there an error in the program that I am not picking up?

Last edited by digitalsystems; November 8th, 2013 at 05:22 AM.
Reason: including code

Re: Pangram Question (All the letters of the alphabet in a sentence)

Did you review the String API page? Did you happen to notice the charAt() method? After following PhHein's suggestion, it might occur to you that a char value can be translated to the index of a 26-element array, 0 - 25. From there, you can increment the array element based on the char found in the sentence:

// for each character in the sentence, iterated with sentenceIndex:
charToArrayIndex = sentence.charAt( sentenceIndex )- someOffset;
freqCounterArray[charToArrayIndex]++;

After your update: There's always more than one way. If you like yours and you've tested it and it meets the assignment's requirements, stick with it.

--- Update ---

I can't let it go. Does the code you posted even compile? If so, how did you test it and what results did you get? Did you try testing with "now is the time for all good men to come to the aid of their country"? What sentence did you test with to ensure your code correctly identifies a pangram? How about "abc def ghi jkl mno pqr stu vwx yz"?

Re: Pangram Question (All the letters of the alphabet in a sentence)

Yes it does compile, please feel free to try it yourself.

yes I did test it with "the quick brown fox jumps over a lazy dog" ( for which I got a positive response) and "The quick brown fox jumped over a lazy dog" (for which I got a negative response). I also tried "aacdefghijklmnopqrstuvwxyz" (for which I got a negative response). I did after trying a few other inputs find an error in the code as when I enter "abcdefghijklmnopqrstuvwxyz" I got a negative response but if I enter "abcdefghijklmnopqrstuvwxyz + any other letters") I get a positive reponse. I am trying to figure out logically why this is happening.
I will try your other suggestions now

importjavax.swing.JOptionPane;publicclass CSQ2
{publicstaticvoid main (String[] agrs){String sentence =JOptionPane.showInputDialog(null, "Please enter a sentence");String sentenceCopy = sentence;
sentence = sentence.toLowerCase();String result ="";String 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";if(sentence.equals(""))
result ="You have not entered a sentence";elseif(sentence.length()<26)
result ="You have not entered a pangram";else{if(sentence.length()>=26)for(int i =0; i<sentence.length();i++){String alph = alphabet.substring(i,i+1);if(sentence.indexOf(alph)!=-1)
result ="You have entered a pangram";else
result ="You have entered not entered a pangram";}}JOptionPane.showMessageDialog(null, result);}}

Re: Pangram Question (All the letters of the alphabet in a sentence)

Okay. I was able to spend more time with the code you've posted, and it will work. There are two minor changes to make: the contents of the String 'alphabet' and the boundary of the for loop that checks if the user's sentence is a pangram. You can also do some optimizing as PhHein suggested, but it's fundamentally a good design.

Re: Pangram Question (All the letters of the alphabet in a sentence)

I think it is a bad design. One problem I can see is that a StringIndexOutOfBoundsException will occur if the sentence is longer than alphabet. If the issue mentioned above is fixed then it will always say it is a pangram if the sentence contains a 'z'. It will only be the last letter of alphabet that sets the final value of result with all others being overwritten.

--- Update ---

<nitpick>
Also, some of the if statements are pointless. An empty String has a length of 0 which is less than 26 making the check for an empty String redundant. If the String has a length of 26 or greater it must enter the else branch therefore the inner if statement is also redundant.
</nitpick>

Re: Pangram Question (All the letters of the alphabet in a sentence)

One problem I can see is that a StringIndexOutOfBoundsException will occur if the sentence is longer than alphabet.

Without giving away the answer, the problem you've highlighted will be addressed by my suggestion to change "the boundary of the for loop." Almost giving the answer away, the OP misunderstands which length drives the number of passes through the for loop.

If the issue mentioned above is fixed then it will always say it is a pangram if the sentence contains a 'z'. It will only be the last letter of alphabet that sets the final value of result with all others being overwritten.

I don't get this point, but maybe it's because I'm seeing the final solution and you're not.

Also, some of the if statements are pointless. An empty String has a length of 0 which is less than 26 making the check for an empty String redundant. If the String has a length of 26 or greater it must enter the else branch therefore the inner if statement is also redundant.

One was the OP's validity check for user entry, catching the <return> with no characters entered, the other a preliminary check for a pangram since no sentence less than 26 could be one. Is it an accurate validity check? No, but it isn't a flaw in the design that prevents it from identifying pangrams either. As I mentioned, other opportunities to optimize the final solution exist without significantly changing the fundamental approach.

I think it is a bad design.

The approach is sound and can be used to complete a program that satisfies the requirements as described by the OP.

Re: Pangram Question (All the letters of the alphabet in a sentence)

if (sentence.length()<26)

That if statement will handle empty Strings ie if user hits return without entering anything. Thus the check for an empty String is redundant.

To explain my point about the algorithm not working (assuming the algorithm is check if sentence contains an 'a' ... check if sentence contains a 'z')
User enters "abz"
Does sentence contain an 'a'? Yes > set result to "is a pangram"
Does sentence contain an 'b'? Yes > set result to "is a pangram"
Does sentence contain an 'c'? No > set result to "is NOT a pangram"
Does sentence contain an 'z'? Yes > set result to "is a pangram"

Re: Pangram Question (All the letters of the alphabet in a sentence)

Obviously it has revealed the flaw since they mentioned being confused by the results they are getting. A better solution would be to use a method that returns a boolean: false as soon as it finds a letter that is not in the sentence and true if it gets all the way to the end.

Re: Pangram Question (All the letters of the alphabet in a sentence)

Guys, thank you very much for your responses.
I have taken the commas out of the String alphabet, I had thought that they were needed in a string, have learned my lesson on this.
I have changed the boundary of the for loop to what I think you were indicating and I have also introduced two counters.
I have tested the code initally and will continue to test it to see if I can find errors.
However one things that is going to happen with this code is regardless of when the program realises it has found that a character of the alphabet does not exist within the String sentence it is going to continue to go through the loop.
Is there a way I can set it to exit the loop as soon as it finds this?
Thanks again for the help, my revised code is below.

Re: Pangram Question (All the letters of the alphabet in a sentence)

Explore the break; statement.

Remember that you need 26 yay votes to be a pangram but only one nay to know that the user's sentence is not a pangram. You can cease checking as soon as a nay is determined. Review Junky's last post for a good approach.