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 9 of 9

Thread: Vowels

  1. #1
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Vowels

    Hi,
    this is the question:
    Your program must accept a String (str) & an Integer(pos), returns true if the character at a given position (pos) is a vowel or false otherwise

    Here is my attempt:
    import java.util.*;
     
    public class VowelPositionFromString {
    	//accepts a String (str) & an Integer(pos), returns true if the character at a given position (pos) is a vowel or false otherwise
    		public static void main (String[]args){
    			Scanner in = new Scanner (System.in);
    			String str;
    			int pos, letter,letterPosition=0;
    			System.out.printf("Enter a string\n");
    			str = in.nextLine();
    			System.out.printf("Enter an intreger position\n");
    			pos = in.nextInt();
     
    			for (int i=0; i<str.length(); i++)
    			{
    				letter= str.charAt(i);
    				if (Character.isUpperCase(letter))
    				{
    					letterPosition =letter - 'A' +1;
    					if(letterPosition == pos)
    					{
    						System.out.printf("True"); 
    					}
    				}		
    				else if (Character.isLowerCase(letter))
    				{
    					letterPosition =letter - 'a' +1;
    					if(letterPosition == pos)
    					{
    						System.out.printf("True"); 
    					}
    				}
    			}
     
     
    		}
    }

    The issue is that I don't know how to link it to a vowel position.
    I was thinking about declaring 2 contants:
    1. vowelPositions for uppercase letters
    2. vowelPositions for lowercase letters
    Then I would know how to incorporate it in the program.


    I hope I explained well, please assist.


    So far my program returns true if a letter in the string corresponds with the position entered.


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Vowels

    take a look in the String class of java String (Java Platform SE 7 ) there is a toLowerCase and toUpperCase method in String class. Character class Character (Java Platform SE 7 ) also have those methods.

    That way, you can compare two characters in a case insensitive way.
    Example
    Character.toLowerCase('A').equals(Character.toLowe rCase('a'))

    There is also a method equalsIgnoreCase in String that you can also use for comparison of two strings in a case insensitive way.
    You can also use the contains method of String but make sure to use the toLowerCase and toUpperCase method.
    Example

    String name = "JAVA GIRL";
    System.out.println(name.contains("java".toUpperCas e());


    as you can see from given examples and methods, there are many ways to do that.

  3. #3
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vowels

    Thank you, I implemented the isLowerCase method well.
    However, the program requires comparing the entered position which is an integer to characters in the string that may be vowels.

    Here is my updated code:
    import java.util.*;
     
    public class VowelPositionFromString {
    	//accepts a String (str) & an Integer(pos), returns true if the character at a given position (pos) is a vowel or false otherwise
    		public static void main (String[]args){
    			Scanner in = new Scanner (System.in);
    			String str;
    			int pos, letter,letterPosition=0;
    			System.out.printf("Enter a string\n");
    			str = in.nextLine();
    			System.out.printf("Enter an intreger position\n");
    			pos = in.nextInt();
     
    			for (int i=0; i<str.length(); i++)
    			{
    				//converts all string letters to lower case
    				letter= str.toLowerCase().charAt(i);
    				letterPosition =letter - 'a' +1;
    					if(pos == letterPosition && pos == 1|| 5 || 9 || 15 || 21)
    					{
    						System.out.printf("True"); 
    					}
    					else 
    					{
    						System.out.printf("False"); 
    					}
    				}		
     
    		}
    	}

    My only problem is the format of the line: if(pos == letterPosition && pos == 1|| 5 || 9 || 15 || 21)
    NB: 1,5,9,15, AND 21 are the vowel positions in lowercase so the position entered (pos) must be equal to the letter postion in the string and any one of the vowel positions to be true.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Vowels

    problem is the format of the line: if(pos == letterPosition && pos == 1|| 5 || 9 || 15 || 21)
    The expressions on each side of a boolean operator like the || must be a boolean expression (returns either true or false). For example: (a > 3) || (b <=2)
    5 is does not have a boolean value. The code needs to compare it to something.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Vowels

    if(pos == letterPosition && pos == 1|| 5 || 9 || 15 || 21)
    I am not sure if you have a compilation error with that line. that is not a valid syntax for java 6 and below, I am not sure if you are using java 7 and you compiled that statement successfully.
    do you have compilation error?

    you can also use the example statement below:
    pos == 1 || pos == 2 || pos == 3 .......

    1,5,9,15, AND 21 are the vowel positions in lowercase so the position entered (pos) must be equal to the letter postion in the string and any one of the vowel positions to be true.
    hmm. but where is the letter or String values that holds that letters? did you declared that a = 1?
    or String letters = "abcd....";? actually you can use the ASCII which says that lower case "a" is equal = 97 (in decimal) kindly see the ASCII tables. that will make you code it easily rather than declaring a String that holds the Alphabet.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,010
    Thanks
    52
    Thanked 2,276 Times in 2,247 Posts

    Default Re: Vowels

    ower case "a" is equal = 97
    That should be 'a' to show its a char and not a String.
    chars convert to int values for comparing. You don't have to look up the ASCII value. You can code: 'a'
    Instead of 1 use 'a', for 5 use 'e', etc for 'i', 'o' and 'u'
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Java girl (April 14th, 2014)

  8. #7
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vowels

    Hi, I took both the ASCII guidance into condieration

    what I changed:
    for (int i=0; i<str.length(); i++)
    			{
    				//converts all string letters to lower case
    				letter= str.toLowerCase().charAt(i);
    				letterPosition =letter - 'a' +1;
    					if(pos == letterPosition) 
    					{
    						if ( letterPosition == 97|| letterPosition == 101 ||letterPosition ==105 || letterPosition== 111 || letterPosition==117)
    						System.out.printf("True"); 
    					}
    					else if (pos != letterPosition)
    					{
    						System.out.printf("False"); 
    					}
    			}

    My output:
    Enter a string
    trend
    Enter an intreger position
    101
    FalseFalseFalseFalseFalse



    It just suppose to say True because 101 is e in ASCII :/

  9. #8
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Vowels

    nope. try to put of the position of e in your first input trend.

    I think It should be like this:
    Enter a string
    Java Girl -my input
    Enter an integer position
    1 -my input


    if it is base 0, then the letter in position 1 is a and the output should be true.
    should it be like that?

  10. The Following User Says Thank You to dicdic For This Useful Post:

    Java girl (April 14th, 2014)

  11. #9
    Member
    Join Date
    Mar 2014
    Posts
    92
    My Mood
    Inspired
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Vowels

    Thank you so much for all the guidance everyone! I simplified the program and got it to work just how it is required to work
    Here is my solution:
    import java.util.*;
     
    public class VowelPositionWithinString {
    	//accepts a String (str) & an Integer(pos), returns true if the character at a given position (pos) is a vowel or false otherwise
    			public static void main (String[]args){
    				Scanner in = new Scanner (System.in);
    				String str;
    				char letter;
    				int pos, vowelPosition=0;
    				System.out.printf("Enter a string\n");
    				str = in.nextLine();
    				System.out.printf("Enter an intreger position\n");
    				pos = in.nextInt();
     
    				for (int i=0; i<str.length(); i++)
    				{
    					//converts all string letters to lower case
    					letter = str.toLowerCase().charAt(i);
                        if (letter == 'a' ||letter == 'e' ||letter == 'i' ||letter == 'o' ||letter == 'u')
                        {
                        	vowelPosition = letter -'a' + 1;
                        	if (vowelPosition == pos)
                        	{
                        		System.out.printf("True");
                        		break;
                        	}
                        	else System.out.printf("False"); 
                        }
     
    		}
     
    	}
    }

Similar Threads

  1. How to replace the vowels with a user input Character
    By gdoggson in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 4th, 2013, 04:53 AM
  2. Using a Loop and Nested Ifs to Count Number of Vowels in a String
    By Potat in forum Loops & Control Statements
    Replies: 17
    Last Post: February 21st, 2013, 08:35 PM
  3. Counting Vowels and Consonants in a String.
    By Andyandhisboard in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 8th, 2013, 08:59 PM
  4. [SOLVED] Program to find how many letters start with vowels
    By Lokesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 14th, 2011, 04:58 AM
  5. Counting Vowels and Prepositions in a text file
    By maybach230 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 27th, 2010, 07:36 PM