Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with a program that is sensing vowels and consonants  RSS feed

 
Braydon Nelson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about something like
 
Braydon Nelson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
// write your code here
        String userInput;
        do {
            Scanner scan = new Scanner(System.in);

            System.out.println("Enter a letter (Q to quit): ");
            userInput = scan.next();

            if (userInput.equals("a") || userInput.equals("A") || userInput.equals("e") || userInput.equals("E") ||
                    userInput.equals("i") || userInput.equals("I") || userInput.equals("o") || userInput.equals("O") ||
                    userInput.equals("u") || userInput.equals("U"))
            {
                System.out.println("This is a Vowel");
            }
            else if (userInput.equals("b") || userInput.equals("B") || userInput.equals("c") || userInput.equals("C") ||
                    userInput.equals("d") || userInput.equals("D") || userInput.equals("f") ||
                    userInput.equals("F") || userInput.equals("g") || userInput.equals("G") ||
                    userInput.equals("h") || userInput.equals("H") || userInput.equals("j") ||
                    userInput.equals("J") || userInput.equals("k") || userInput.equals("K") ||
                    userInput.equals("l") || userInput.equals("L") || userInput.equals("m") ||
                    userInput.equals("M") || userInput.equals("n") || userInput.equals("N") ||
                    userInput.equals("p") || userInput.equals("P") || userInput.equals("r") ||
                    userInput.equals("R") || userInput.equals("s") || userInput.equals("S") ||
                    userInput.equals("t") || userInput.equals("T") || userInput.equals("v") ||
                    userInput.equals("V") || userInput.equals("w") || userInput.equals("W") ||
                    userInput.equals("x") || userInput.equals("X") || userInput.equals("y") ||
                    userInput.equals("Y") || userInput.equals("z") || userInput.equals("Z"))
            {
                System.out.println("This is a Consonant");
            }
            else if (userInput.equals("q") || userInput.equals("Q"))    // i added this line to in and now it works. the break let it happen
            {
                System.out.println("This is a Consonant. Goodbye");
                break;
            }
            else {
                System.out.println("!!ERROR TRY AGAIN!!");
            }
        } while (true);

    }
}
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while (userInput != 'q' || userInput != 'Q'); 

The OR operator returns true if EITHER of the expressions is true.  So if input is equal to q then its not equal to Q
The AND operator returns true only if BOTH of the expressions are true.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider using Character.isLetter(userInput) instead of all those if-checks. If that is true, then you only have to check for the vowels to determine whethefr it is a consonane or a vowel. Also, if this is something other than a programming exercise, in English, the letter 'y' is often a vowel (and, rarely, 'w' can also be a vowel)
 
Campbell Ritchie
Marshal
Posts: 55677
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags to your first post. Always use the tags: doesn't it look better
Please explain what the problem is; you have not actually asked a question. Norhave you explained how your second post differs from the first; I nearly deleted it thinking it was a mistaken duplicate.
Move the declaration of the Scanner before the loop; you don't need more that one Scanner object.
Why are you using scan.next() and testing for equality with those one‑letter Strings? What was wrong with the first version with chars? Why are you going to no end of trouble to test for vowels and later using else if rather than simple else?

It looks wrong to me to have a sentinel value Q which is a normal result. Why not use 0 as a sentinel to quit?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!