Jump to content

Java- How to Find a vowel

Beeeyeee

I'm writing a program that will take a word the user inputs, then translate it to pig latin 

(in case you dont know:  For words that begin with consonants, the initial consonant or consonant cluster is moved to the end of the word, and "ay" is added, as in the following examples  "happy" → "appyhay"  "glove" → "oveglay"  "school" → "oolschay" For words that begin with vowels, "way" is added at the end of the word. Examples are  "egg" → "eggway"  "inbox" → "inboxway"  "eight" → "eightway")

 

anyway i'm having trouble figuring out how to tell if the first letter is a vowell or consonant. how do you do that?


import java.util.Scanner;

public class Operation4
{
   public static void main (String[] args)
   {
      Scanner in = new Scanner(System.in);
      
      String word = "";
      String firstLetter = "";
      String pigLatin = "";
      
      //get word from user
      System.out.println("Hello! Enter any word and we'll professionally translate it for you:");
      word = in.nextLine();
      
      //trim off any white space and puncuation
      word = word.trim(); 
      word = word.replaceAll("[^a-zA-Z\\s]", "");
      
      firstLetter = word.charAt(0); //firstLetter = first letter of 'word'
      
      //detect if first letter is a vowell or consonant
      if (firstLetter.equalsIgnoreCase = ) //if firstLetter = a vowell
      {
         pigLatin = (word + "way");
      }
      else
      {
         //***move vowell to end of sentence + "ay" ***
      }
      
      //***capitolize first letter, lowercase all other letters***
            
      System.out.println(pigLatin);
      
   }
}

Link to comment
Share on other sites

Link to post
Share on other sites

if(firstLetter.toLowerCase() == 'a' || firstLetter.toLowerCase() == 'e' || ......){

      //stuff

}

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, djdwosk97 said:

 


if(firstLetter.toLowerCase == 'a' || firstLetter.toLowerCase == 'e' || ......){

      //stuff

}

 

Thank you sir! hope this works. 

Link to comment
Share on other sites

Link to post
Share on other sites

Unless the String class has a built in function to distinguish between consonants and vowels, you'll need to create your own method.

Quote or tag if you want me to answer! PM me if you are in a real hurry!

Why do Java developers wear glasses? Because they can't C#!

 

My Machines:

The Gaming Rig:

Spoiler

-Processor: i5 6600k @4.6GHz

-Graphics: GTX1060 6GB G1 Gaming

-RAM: 2x8GB HyperX DDR4 2133MHz

-Motherboard: Asus Z170-A

-Cooler: Corsair H100i

-PSU: EVGA 650W 80+bronze

-AOC 1080p ultrawide

My good old laptop:

Spoiler

Lenovo T430

-Processor: i7 3520M

-4GB DDR3 1600MHz

-Graphics: intel iGPU :(

-Not even 1080p

 

Link to comment
Share on other sites

Link to post
Share on other sites

Okay that part worked I think, but i'm having trouble with Strings & chars. 

 

if I make firstLetter a String, I get 3 errors: "incompatible types", and two "cannot find symbol"'s

but if I make firstLetter a char, I get 2 errors: "a char cannot be derefrenced" and another "cannot find symbol"

the "cannot find symbol"s should go away once this is worked out.

 

but I guess i'm having difficulty converting a char to a string or vice versa and I don't quite understand what i'm doing wrong. I've been researching to no avail. 


import java.util.Scanner;

public class Operation4
{
   public static void main (String[] args)
   {
      Scanner in = new Scanner(System.in);
      
      String word = "";
      char firstLetter = ' '; 
      //String firstLetter = "";
      String pigLatin = "";
      
      //get word from user
      System.out.println("Hello! Enter any word and we'll professionally translate it for you:");
      word = in.nextLine();
      
      //trim off any white space and puncuation
      word = word.trim(); 
      word = word.replaceAll("[^a-zA-Z\\s]", "");
      
      //firstLetter = first letter of 'word'
      firstLetter = word.charAt(0);  
           
      //detect if first letter is a vowell or consonant
      if (firstLetter.toLowerCase == 'a') // || firstLetter.toLowerCase == 'e'  || firstLetter.toLowerCase == 'i'  || firstLetter.toLowerCase == 'o'  || firstLetter.toLowerCase == 'u'
      {
         pigLatin = (word + "way");
      }
      else
      {
        pigLatin = (word + firstLetter + "ay"); //***move vowell to end of sentence + "ay" ***
      }
      
      //***capitolize first letter, lowercase all other letters***     
      pigLatin.toLowerCase = pigLatin.substring(0, 1).toUpperCase() + pigLatin.substring(1);
     
            
      System.out.println(pigLatin);
      
   }
}

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Beeeyeee said:

Okay that part worked I think, but i'm having trouble with Strings & chars. 

 

if I make firstLetter a String, I get 3 errors: "incompatible types", and two "cannot find symbol"'s

but if I make firstLetter a char, I get 2 errors: "a char cannot be derefrenced" and another "cannot find symbol"

the "cannot find symbol"s should go away once this is worked out.

 

but I guess i'm having difficulty converting a char to a string or vice versa and I don't quite understand what i'm doing wrong. I've been researching to no avail. 

 

 

toLowerCase() is a string function, not a char function, I wasn't paying attention. 

 

Do: 

String lowerCase = word.toLowerCase();

char firstLetter = lowerCase.charAt(0);


//or if you don't need to preserve the original capitalizations, just:
word.toLowerCase(); 
char firstLetter = word.charAt(0);

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Beeeyeee said:

I'm writing a program that will take a word the user inputs, then translate it to pig latin 

This is a perfect situation to use Regex.

The following example returns the first vowel in a word. A more complex pattern could return all the vowels in a word. Regex is difficult to me, and I couldn't come up with the exact pattern for your uses. Sorry. An alternative to a more complex Regex would be to use a simple regex to determine if a character is a vowel, and then iterate through all the characters in a string. If you did that though, you might as well check the character against a dictionary that holds all the vowels.

Pattern p = Pattern.compile("[aeiouy]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("Your_string_here");

if (m.find()) {
  // found a vowel
}

else {
  // Didn't find a vowel.
  
	

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

this should also work

 

string vowelCharacters = "aeiouAEIOU";

if(vowelCharacters.contains(firstLetter)){
...
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, straight_stewie said:

This is a perfect situation to use Regex.

The following example returns the first vowel in a word. A more complex pattern could return all the vowels in a word. Regex is difficult to me, and I couldn't come up with the exact pattern for your uses. Sorry. An alternative to a more complex Regex would be to use a simple regex to determine if a character is a vowel, and then iterate through all the characters in a string. If you did that though, you might as well check the character against a dictionary that holds all the vowels.


Pattern p = Pattern.compile("[aeiouy]", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("Your_string_here");

if (m.find()) {
  // found a vowel
}

else {
  // Didn't find a vowel.
  
	

 

I seriously doubt the OP knows or should know regex right now.

Keeping it simple and dumb is just fine.

And for that, I could think of 2 options:

public static boolean isVowel(char c) {
  return "AEIOUaeiou".indexOf(c) != -1;
}

or

public static boolean isVowel(char c) {
  return c=='a' || c=='e' || c=='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U';
}

 

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×