• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help with a program that is sensing vowels and consonants

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about something like
 
Braydon Nelson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

   }
}
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Marshal
Posts: 79061
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
reply
    Bookmark Topic Watch Topic
  • New Topic