mercredi 2 septembre 2015

How to convert user input string to pig latin?

Im trying to convert a user entered string that starts with a consonant to pig latin buy moving all the consonants to the end of the word till the word starts with a vowel, and then adding "ay" to the end of the word. I have a for loop that's supposed to do this, but for some reason, it outputs nothing. What am i doing wrong here? Im stumped.

Here's the code:

import java.util.Scanner;

public class two {
    public static void main(String[] args) {

        System.out.println("Please enter a word");
        Scanner word = new Scanner(System.in);
        String pigLatin = word.nextLine();
        while (!pigLatin.equalsIgnoreCase("quit")) {
            if (isVowel(pigLatin.charAt(0))) {
                pigLatin = (pigLatin + "way");
                System.out.println(pigLatin);
            } 
            else {
                for (int i = 0; i < pigLatin.length(); i++) {
                    char firstChar = pigLatin.charAt(0); 
                    pigLatin = pigLatin.substring(1);
                    pigLatin = pigLatin + firstChar;
                    if (i >= pigLatin.length())
                    {
                        pigLatin = pigLatin + "ay";
                        System.out.println(pigLatin);
                    }
                }

            }

            System.out.println("Please enter a word");
            pigLatin = word.nextLine();
        }
        word.close();

    }

    private static boolean isVowel(char ch) {
        char v = Character.toLowerCase(ch);
        if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u') {
            return true;
        }

        else {
            return false;
        }

    }

}

Aucun commentaire:

Enregistrer un commentaire