Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Using your New Tools Coding the Prototype

Derek Derek
Derek Derek
8,744 Points

Determining if the first letter of the adjective is a vowel or not

Hello,

After watching this video, I tried to fix one of the bugs, but I keep failing and my result does not change.

I first want to determine if the first word of the adjective is a vowel. If so, I want to change the 'a' to 'an'.

Below is my code. Thank you for your help!

import java.io.Console;

public class TreeStory {
    public static void main(String[] args) {
        Console console = System.console();

        String name = console.readLine("Enter a name:  ");
        String adjective = console.readLine("Enter an adjective:  ");
        String noun = console.readLine("Enter a noun:  ");
        String adverb = console.readLine("Enter an adverb:  ");
        String verb = console.readLine("Enter a verb ending with -ing:  ");

        String vowelOrNot = determineVowel(adjective);

        console.printf("Your TreeStory:\n----------------\n");
        console.printf("%s is %s %s %s.   ", name, vowelOrNot, adjective, noun);
        console.printf("They are always %s %s.\n", adverb, verb);
    }

    public static String determineVowel(String adjective) {

        String vowelOrNot = "a";

        if (
            String.valueOf(adjective.charAt(0)) == "a" || 
            String.valueOf(adjective.charAt(0)) == "e" || 
            String.valueOf(adjective.charAt(0)) == "i" || 
            String.valueOf(adjective.charAt(0)) == "o" ||
            String.valueOf(adjective.charAt(0)) == "u"
            ) 
        {
            vowelOrNot = "an";
            return vowelOrNot;
        }
        return vowelOrNot;
    }
}

1 Answer

It wasn't working because, you're using ==, and not the .equals() method. The == operator determines if 2 references point to the same object.

import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {
        Console console = System.console();

        String name = console.readLine("Enter a name:  ");
        String adjective = console.readLine("Enter an adjective:  ");
        String noun = console.readLine("Enter a noun:  ");
        String adverb = console.readLine("Enter an adverb:  ");
        String verb = console.readLine("Enter a verb ending with -ing:  ");

        String vowelOrNot = determineVowel(adjective);

        console.printf("Your TreeStory:\n----------------\n");
        console.printf("%s is %s %s %s.   ", name, vowelOrNot, adjective, noun);
        console.printf("They are always %s %s.\n", adverb, verb);
    }

    public static String determineVowel(String adjective) {
        String vowelOrNot = "a";
        if(String.valueOf(adjective.charAt(0)).equals("a"))
        {
            vowelOrNot = "an";
            return vowelOrNot;
        } else {
            return vowelOrNot;
        }
    }
}

No problem

Also, just as a bit of extra functionality. Instead of using .equals() method you could use the equalsIgnoreCase() method, then your vowelOrNot String will always be changed, regardless, if the user types the adjective with a capital vowel or not. If you continue to use the .equals() method, when the user types in an adjective with a capital vowel then vowelOrNot will always be "a", and not "an".

import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {
        Console console = System.console();

        String name = console.readLine("Enter a name:  ");
        String adjective = console.readLine("Enter an adjective:  ");
        String noun = console.readLine("Enter a noun:  ");
        String adverb = console.readLine("Enter an adverb:  ");
        String verb = console.readLine("Enter a verb ending with -ing:  ");
        String vowelOrNot = determineVowel(adjective);

        console.printf("Your TreeStory:\n----------------\n");
        console.printf("%s is %s %s %s.   ", name, vowelOrNot, adjective, noun);
        console.printf("They are always %s %s.\n", adverb, verb);
    }

    public static String determineVowel(String adjective) {
        String vowelOrNot = "a";
        if(String.valueOf(adjective.charAt(0)).equalsIgnoreCase("a"))
        {
            vowelOrNot = "an";
            return vowelOrNot;
        } else {
            return vowelOrNot;
        }
    }
}