Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

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

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    import javax.swing.JOptionPane;
    public class CSQ2
    {
    	public static void 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";
    		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 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);
     
    	}
    }


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

    Default 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.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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?

    import javax.swing.JOptionPane;
    public class CS5051Q2
    {
    	public static void 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

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default 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"?

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    import javax.swing.JOptionPane;
    public class CSQ2
    {
    	public static void 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);
     
    	}
    }

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

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

    I'm not sure this approach will ultimately prevail, but I'll play along a bit longer. What are the commas in the String alphabet? Are they necessary? Are they helping? Hurting?

  8. #8
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    589
    My Mood
    Sleepy
    Thanks
    10
    Thanked 87 Times in 80 Posts

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

    What does this line return for i = 1?
    String alph = alphabet.substring(i, i + 1);
    When you enter the else (i.e. sentence.indexOf(alph) == -1) you don't need to continue your loop.

    And you still have if esles without braces.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default 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.

    Good job!

  10. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default 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>
    Improving the world one idiot at a time!

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default 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.

  12. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default 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"

    End result will be "abz" is a pangram when it clearly isn't.
    Improving the world one idiot at a time!

  13. #13
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

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

    Ahhh, I see. I corrected this logic error in my implementation of the design. Hopefully, the OP's own testing would reveal this flaw.

  14. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,542
    My Mood
    Grumpy
    Thanks
    0
    Thanked 166 Times in 157 Posts

    Default 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.

    Pseudocode
    loop a - z {
        if current letter not in sentence {
            return false;
        }
    }
    return true;
    Improving the world one idiot at a time!

  15. #15
    Junior Member
    Join Date
    Oct 2013
    Posts
    21
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.

    public class CSQ2
    {
    	public static void main (String [] agrs)
    	{
    		String sentence = JOptionPane.showInputDialog(null, "Please enter a sentence");
    		String sentenceCopy = sentence;
    		String aLetter = "";
    		sentence = sentence.toLowerCase();
    		String result = "";
    		String alphabet = "abcdefghijklmnopqrstuvwxyz";
    		int counterPos = 0;
    		int counterNeg = 0;
    		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<alphabet.length();i++)
    			{
    				String alph = alphabet.substring (i,i+1);
    				if(alph.indexOf(sentence)!=-1)
    				counterPos++;
    				else
    					{
    					if(sentence.indexOf(alph)==-1)
    				counterNeg++;
    					}			
    				}
     
    			if(counterNeg>0)
    			result = "You have not entered a pangram";
    			else
    			result = "You have entered a pangram";
    		}
    		JOptionPane.showMessageDialog(null, result);
     
    	}
    }

  16. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,045
    My Mood
    Mellow
    Thanks
    184
    Thanked 649 Times in 636 Posts

    Default 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.

Similar Threads

  1. Alphabet Program
    By l8kernation in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2013, 11:38 PM
  2. Replies: 12
    Last Post: March 17th, 2013, 01:38 AM
  3. Hangman java question double letters
    By sparky971 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2012, 01:01 PM
  4. Easy Help...Printing out the alphabet.
    By Jsmooth in forum Loops & Control Statements
    Replies: 13
    Last Post: April 15th, 2012, 07:28 PM
  5. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM