Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,640 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,076 people online right now. Registration is fast and FREE... Join Now!




Vowel/consonant issue

 
Reply to this topicStart new topic

Vowel/consonant issue, shortening length of code

ProGraM
post 4 Sep, 2008 - 07:09 PM
Post #1


D.I.C Head

**
Joined: 28 Sep, 2005
Posts: 91



Thanked 2 times
My Contributions


My program runs and works I just need help with shortening a part of my code, Instead of typing all the consonants out. If there is a shorter way of doing please help, and if there are any other problems or anything please tell me.

CODE
import java.io.*;//Provides for system input and output through data streams
import javax.swing.JOptionPane;//Imports Dialog box in program
public class VowelsConsonants
{
    public static void main (String[] args)
    {
        int choice, again;
        choice = JOptionPane.showConfirmDialog (null, "Would you like to play the Vowel/Consonant game?, Please Choose Yes, No, or Cancel!");//user decides wether to play game                                            
        String letter = " ";//ables user to input a letter
            if (choice == JOptionPane.YES_OPTION)//user plays the game if he/she chooses the "Yes" option.
            {
                do
                {
                    letter = JOptionPane.showInputDialog ("Please enter a letter from A-Z: ");//user enters a letter from a-z
                    if (letter.equalsIgnoreCase("a") || letter.equalsIgnoreCase("e") || letter.equalsIgnoreCase("i") || letter.equalsIgnoreCase("o") || letter.equalsIgnoreCase("u"))//tells the program the vowels, so it can decide if its a vowel or consonant
                    {
                        JOptionPane.showMessageDialog (null, "You have entered a vowel.");//tells the user whether he/she entered a vowel or not
                    }
                    else if (letter.equalsIgnoreCase("b") || letter.equalsIgnoreCase("c") || letter.equalsIgnoreCase("d") || letter.equalsIgnoreCase("f") || letter.equalsIgnoreCase("g") || letter.equalsIgnoreCase("h") || letter.equalsIgnoreCase("j") || letter.equalsIgnoreCase("k") || letter.equalsIgnoreCase("l") || letter.equalsIgnoreCase("m") || letter.equalsIgnoreCase("n") || letter.equalsIgnoreCase("o") || letter.equalsIgnoreCase("q") || letter.equalsIgnoreCase("r") || letter.equalsIgnoreCase("s") || letter.equalsIgnoreCase("t") || letter.equalsIgnoreCase("v") || letter.equalsIgnoreCase("w") || letter.equalsIgnoreCase("x") || letter.equalsIgnoreCase("y") || letter.equalsIgnoreCase("z"))
                    {
                        JOptionPane.showMessageDialog (null, "You have entered a consonant.");//tells the user whether he/she entered a consonant or not
                    }
                    else
                    {
                        JOptionPane.showMessageDialog (null, "Error: Invalid Input, No Symbols, Numbers, or Spaces Please");
                    }            
                    again = JOptionPane.showConfirmDialog (null, "Would you like to try again? ");//ask user if he/she wants to try again
                }while (again == JOptionPane.YES_OPTION);//user plays the game until they choose "No" or "Cancel"
            }
    }
}            
User is offlineProfile CardPM

Go to the top of the page

pbl
post 4 Sep, 2008 - 07:42 PM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


I guess you can use 2 arrays of char for the choices

CODE


import javax.swing.JOptionPane;//Imports Dialog box in program

public class VowelsConsonants
{
    public static void main (String[] args)
    {
        int choice, again = 0;
        char[] voyel = {'a', 'e', 'i', 'o', 'u'};
        char[] consonant = {'b', 'c', 'd', 'f', 'g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'};
        choice = JOptionPane.showConfirmDialog (null, "Would you like to play the Vowel/Consonant game?, Please Choose Yes, No, or Cancel!");//user decides wether to play game                                            
        char letter;
        if (choice == JOptionPane.YES_OPTION)//user plays the game if he/she chooses the "Yes" option.
        {
            do
            {
                boolean typeFound = false;
                String str = JOptionPane.showInputDialog ("Please enter a letter from A-Z: ");//user enters a letter from a-z
                str = str.toLowerCase();
                letter = str.charAt(0);
                for(int i = 0; i < voyel.length; i++) {
                    if(letter == voyel[i]) {
                        JOptionPane.showMessageDialog (null, "You have entered a vowel.");//tells the user whether he/she entered a vowel or not
                        typeFound = true;
                        break;
                    }
                }
                // OK was a vowel
                if(typeFound)
                    continue;

                for(int i = 0; i < consonant.length; i++) {
                    if(letter == consonant[i]) {
                        JOptionPane.showMessageDialog (null, "You have entered a consonant.");//tells the user whether he/she entered a consonant or not
                        typeFound = true;
                        break;
                    }
                }
                // OK was a consonant
                if(typeFound)
                    continue;
                JOptionPane.showMessageDialog (null, "Error: Invalid Input, No Symbols, Numbers, or Spaces Please");

                again = JOptionPane.showConfirmDialog (null, "Would you like to try again? ");//ask user if he/she wants to try again
            }while (again == JOptionPane.YES_OPTION);//user plays the game until they choose "No" or "Cancel"
        }
    }
}            

User is offlineProfile CardPM

Go to the top of the page

baavgai
post 5 Sep, 2008 - 03:57 AM
Post #3


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,967



Thanked 96 times

Dream Kudos: 475

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


You've already got the logic of vowels. If you have the logic of "all letters" you're pretty much done.

Here's one approach.
java

import java.io.*;
import javax.swing.JOptionPane;
public class VowelsConsonants {
public static void main (String[] args) {
int choice = JOptionPane.showConfirmDialog (null,
"Would you like to play the Vowel/Consonant game?, Please Choose Yes, No, or Cancel!"
);
while (choice == JOptionPane.YES_OPTION) {
String input = JOptionPane.showInputDialog ("Please enter a letter from A-Z: ");
String response;
if (input==null || input.length()!=1) {
response = "Please one letter only";
} else {
char letter = input.toLowerCase().charAt(0);
if (letter<'a' || letter>'z') {
response = "Error: Invalid Input, No Symbols, Numbers, or Spaces Please";
} else if (letter=='a' || letter=='e' || letter=='i' || letter=='o' || letter=='u') {
response = "You have entered a vowel.";
} else {
response = "You have entered a consonant.";
}
}
JOptionPane.showMessageDialog (null, response);
choice = JOptionPane.showConfirmDialog (null, "Would you like to try again? ");
}
}
}


Hope this helps.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:31AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month