Hello, all. The following is a problem I'm currently working on. I can't get it to run like the problem asks. Any help would be greatly appreciated.

Recommended Answers

All 5 Replies

Here is the code I have so far:

package letterclass;

import java.util.Scanner;

public class LetterClass {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
       String a;
       Scanner input = new Scanner (System.in);
       System.out.print("Please enter a string: ");
       a = input.nextLine();

       for (char i = 0; i < a.length(); i++)
    {            
            if (a.equalsIgnoreCase(a))
        {
                    System.out.print("Vowel");
                    switch(i)
                    {
                        case 'a':
                        case 'e':
                        case 'i':
                        case 'o':
                        case 'u':
                    }
        }
            else if (a.equalsIgnoreCase(a))
                {
                    System.out.print("Semi-Vowel");
                    switch(i)
                    {
                        case 'y':
                        case 'w':
                    }
                }    
            else
                {
                    System.out.print("Consonant");
                }    
        }
    }

Hello there, I can see that you don't quite get how arrays and traversing them work. Not to worry !!!

Imagine a restaurant menu where the dishes are numbered. For example :
1. Chicken
2. Beef
3. Fish
...
Then when you want to order you can just say "Can I have number 2 pls" and you will get a beef dish. In the world of Java you request will look like :
Array menu;
menu[1];
(note that we start counting from 0 and not from 1);
Now if you want to order every item in the menu (maybe you are very hungry)
you would say: "Can I have number 1, number 2 and number 3". In Java world this will be :
menu[0];menu[1];menu[2]; Yeah but I don't want to write this line so many times and moreover i dont know how long the menu is (menu.lenght). So lets loop over the items.

 for(int i=0; i<menu.lenght;i++){
      order.menu[i];
 }

done deal !!
That is partly how the arrays work.
And here is your working program:

import java.util.Scanner;
public class LetterClass {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) 
    {
       char[] a;
       Scanner input = new Scanner (System.in);
       System.out.print("Please enter a string: ");
       a = input.nextLine().toCharArray();
       for (char i = 0; i < a.length; i++) {
           if(a[i]=='a' || a[i]=='e' || a[i]=='i' || a[i]=='o' || a[i]=='u') {
               System.out.println("Vowel : " + a[i]);
           } else if (a[i]=='w' || a[i]=='y') {
               System.out.println("Semi-Vowel: " + a[i]);
           } else {
               System.out.println("Consonant: " + a[i]);
           }
       }
    }
}

I see exactly what you're doing here, but the main purpose of this program was to utilize a switch statement which is why I had it set up the way I did. I see that you got rid of the switch statement completely. Is there a way to integrate it in?

This uses the switch you want and will only allow letters(will not accept numbers or a string longer than one, but could not figure out how to eliminate symbols):

package letterclass;

import java.util.Scanner;

public class LetterClass {

    public static void main( String[] args ) 
    {

       Scanner input = new Scanner (System.in);
       System.out.print("Please enter a letter: ");
       String inputStr = input.next();
       char letter = inputStr.charAt(0);

        if (inputStr.length() > 1 || Character.isDigit(inputStr.charAt(0))) {
            System.out.println("Enter a letter!");

        }else{
                    switch(letter)
                    {
                        case 'a':
                            System.out.print("Vowel");
                            break;
                        case 'e':
                            System.out.print("Vowel");
                            break;
                        case 'i':
                            System.out.print("Vowel");
                            break;
                        case 'o':
                            System.out.print("Vowel");
                            break;
                        case 'u':
                            System.out.print("Vowel");
                            break;
                        case 'y':
                            System.out.print("Semi-Vowel");
                            break;
                        case 'w':
                            System.out.print("Semi-Vowel");
                            break;
                        default:
                            System.out.print("Consonant");
                            break;
                }    
            }
        input.close();
       }


}

Java 7 allows switches using a String. If you're not on Java 7 then you should be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.