Java Java Basics Getting Started with Java Receiving Input

Yusuf Mohamed
Yusuf Mohamed
2,620 Points

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
STAFF
Jennifer Nordell
Treehouse Staff

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:

Yusuf Mohamed
Yusuf Mohamed
2,620 Points

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
Jennifer Nordell
Treehouse Staff

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:

Yusuf Mohamed
Yusuf Mohamed
2,620 Points

Thank you very much for the help. : )