1

I'm trying to use a for loop to examine each character in an array and print the character, it's position in the array, and what type of character it is (vowel, consonant, etc.). I have this so far:

char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'};

         System.out.print("\nMy name is: ");

            for(int index=0; index < myName.length ; index++)
            System.out.print(myName[index]);

            for(char c : myName) {
            if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
            {
                System.out.println("The character located at position is a vowel.");
            }
            else if (c == 'j' || c == 'h' || c == 'n' || c == 'd')
            {
                System.out.println("The character located at position is a consonant.");
            }
            else if (c == ' ')
            {
                System.out.println("The character located at position is a space.");
            }

How do I print the location of the character (i.e. "The character x located at position x is a vowel.")

3
  • what's wrong with what you have? Commented Jun 11, 2011 at 3:20
  • Are you looking for a way to identify if the current letter being examined is a vowel or a consonant? It seems like you're on the right track, but you need to come up with a way to determine that part. Commented Jun 11, 2011 at 3:22
  • I'm not sure how to have it print what type of character it is. Commented Jun 11, 2011 at 3:23

3 Answers 3

3

You're on the right track. Your loop is OK, but try the foreach syntax if you don't actually need the index, like this:

 char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'};

 System.out.print("\nMy name is: ");

 for(char c : myName) {
     System.out.print(c); 
 }

Now add in some logic:

 int i = 0;
 for(char c : myName) {
     i++;
     // Is the char a vowel?
     if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
         // do something - eg print in uppercase
         System.out.print(Character.toUpperCase(c) + " at position " + i);
     } else {
         // do something else - eg print in lowercase
         System.out.print(Character.toLowerCase(c) + " at position " + i);
     }
 }

You must figure out what you want to do here. Now go do it :)

EDITED: To show use of position, which is a little clumsy, but still less code than a standard for loop

Sign up to request clarification or add additional context in comments.

6 Comments

Just a small addition, in case you are really processing on characters, prefer switch case then if.
Thank you. This was really helpful. Just one more question... how would I print the location of the character (i.e. "The character x located at position x is a vowel")
@JDB - this is why the for each loop is not appropriate in this particular example.
@Steven Oh really??? Check out the edit - it has a common pattern to address this,
@Sandy - switch is harder to read and creates more code; with the switch and all the breaks
|
0

Hints:

  • You should use the kind of for loop that you are currently using. The value index variable is going to be useful in your output.

  • The Character class has a number of methods for classifying characters, and for converting from upper case to lower case & vice versa.

  • You can also use == to test characters ...

  • You could also use a switch statement to discriminate between the different kinds of letter, and use the default branch for the rest.

1 Comment

Kind of. You are not yet handling upper case letters, or classifying characters that are not letters, and not spaces. How far you need to go depends on exactly how the problem was stated; i.e. what assumptions you are allowed to make about the input characters. You should now also be able to include the position in the output.
0
     char[] myName = new char[] {'J', 'o', 'h', 'n', ' ', 'D', 'o', 'e'};

     System.out.print("\nMy name is: ");

        for(int index=0; index < myName.length ; index++)
        char c = myname[index];
        if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
        {
            System.out.println("The character " + c + "located at position " + index + " is a vowel.");
        }
    ... }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.