javaconverterswordsjgrasplatin

Converting words to Pig Latin


i hope this finds you well.

I am fairly new to Java and also to this website. Although this may seem long, i only need help with two things, so please help, and like i said I'm new to all of this, so the more thorough the better. I have to do a project where we have to convert regular English words into pig Latin. I'm missing a few things our instructor wants to add. his is the code i have right now.

import java.util.Scanner;
public class PigLatin 
{
public static void main(String[] args) 
{
Scanner sc = new Scanner(System.in);
final String vowels = "aeiouAEIOU";
System.out.println("Enter your word.");
String word = sc.nextLine();
while (!word.equalsIgnoreCase("done"))
{
String beforVowel = "";
int cut = 0;
while (cut < word.length() && !vowels.contains("" + word.charAt(cut)))
{
beforVowel += word.charAt(cut);
cut++;
}
if (cut == 0)
{
cut = 1;
word += word.charAt(0) + "w";
}
System.out.println(word.substring(cut) + beforVowel + "ay");
System.out.println("Enter your word.");
word = sc.nextLine();
}
}
}

The codes I cant seem implement is "If the word has no vowel, print "INVALID"", for example if i type in bcdfgh into this code, it reads back bcdfgh ay. But it should say invalid

one other thing i cant add code for is this "If the first vowel is a "u" and the letter before it is a "q" then the "u" also goes to the end of the word." for example if i type in question into this code, it reads uestionqay. but i want it to say estionquay.

please and thank you


Solution

  • Hey so i coded something up for you...

    import java.util.Scanner;
    
    public class PigLatin 
    {
        public static void main(String[] args) 
        {
            Scanner sc = new Scanner(System.in);
            System.out.println("Enter your word.");
            String word = "";
            word = sc.nextLine();
            do
            {
                if(word.length() > 0)
                {
                    if(containsVowels(word.substring(0,1)))
                    {
                        System.out.println(word+"way");
                    }
                    else
                    {
                        if(containsVowels(word))
                        {
                            System.out.println(word.substring(1,word.length())+word.substring(0,1)+"ay");
                        }
                        else
                            System.out.println("INVALID");
                    }
                }
                System.out.println("Enter your word.");
            }
            while(!((word = sc.nextLine()).equalsIgnoreCase("done")));
            sc.close();
        }
    
        public static boolean containsVowels(String word)
        {
            String[] vowels = {
                    "a","e","i","o","u"
            };
            for(int i = 0; i < vowels.length; i++)
            {
                if(word.contains(vowels[i]) ||  word.contains(vowels[i].toUpperCase()))
                    return true;
            }
            return false;
        }
    }
    

    THIS DOES NOT SOLVE YOUR PROBLEM FOR

    "one other thing i cant add code for is this "If the first vowel is a "u" and the letter before it is a "q" then the "u" also goes to the end of the word." for example if i type in question into this code, it reads uestionqay. but i want it to say estionquay."

    You must try to do that on your own :) I can review what you did to see if it would work but i want to see you try that.