Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Getting Started with Java Receiving Input

Is it possible to add code that changes a/an depending on if the word starts with a vowel or a consonant.

Is it possible to add code that changes a/an depending on if the word starts with a vowel or a consonant.

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Yusuf Mohamed ! I received your request for assistance. Yes, this would be possible and isn't a bad idea. You could check if the first character of the next word matches a vowel and change the indefinite article based on that. And while it will be correct most of the time, it will not be 100% accurate.

As you likely know, English has many exceptions to every rule. Strictly speaking, the indefinite article "a/an" is not determined by the following letter being a consonant or a vowel, rather it is determined by the sound of the next letter. For example, the word "university" begins with a "u", but that "u" is pronounced differently in "university" than it is in "umbrella". We say "an umbrella" but say "a university" and this is dependent upon the sound of that following vowel. Notable exceptions are "u"s as I covered earlier where the u is pronounced as "you". You also have "o" that can be pronounced as "w": an octagon, a one-sided argument. Silent "h"s also abound: an honor, a high school.

Just be aware that to have it be very accurate, you might have to make an exception list of words as well.

Hope this helps! :sparkles:

This helps a lot and I sort of forgot that a/an is dependent on the sound of the word. But, if I would've done it would I first make 2 lists with vowels and consonants then make an if statement if the first letter is a vowel or consonant. But how would I create a condition only looking for the first letter?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Yusuf Mohamed Hi again! Honestly, I would just make a list of the vowels. If it's not in the vowels then it must be a consonant, right? :smiley:

As far as getting the first letter, you could use the charAt(0) and that will return the first letter of any given string. You can find the documentation here

Good luck! :sparkles:

Thank you very much for the help. : )