giigii
giigii

Reputation: 55

Java Split string into individual words for Pig Latin translator

I have a one word Pig Latin translator, and I need it to

The word/string I am using to test this is "Astah la vista, baby." whose output should be "Astahay alay istavay, abybay."

This code only translates one word strings correctly, and does not capitalize/punctuate correctly. My program uses 3 methods, input(), pigLatinator(), and display() to input the string, translate, and display the translation. With this structure, what could I change/add to make this program work closer to the way I would like it? I've looked at using String.split("\\s") but I don't understand how to implement it.

input Method:

    public String input(){
    System.out.println("Please enter your string.");
    System.out.print("String: ");
    originalString = keyboard.nextLine();
    originalString = originalString.toLowerCase(); //String.toLowerCase() to prevent error on line 41 (if() statement)

    return originalString;
}

pigLatinator Method:

    public String pigLatinator(String originalString){
    if(originalString.startsWith("a") || originalString.startsWith("e") || originalString.startsWith("i") || originalString.startsWith("o") || originalString.startsWith("u")){
        pigString = originalString + "ay";
    }else{
        pigString = originalString.substring(1) + originalString.charAt(0) + "ay";
    }

    return pigString;
}

display Method:

public void display(){
    System.out.println();
    System.out.println("Original string: " +originalString);
    System.out.println("Translation: " +pigString);
}

Outputs:

Output 1:

Please enter your string.
String: astah

Original string: astah
Translation: astahay

Works

Output 2:

Please enter your string.
String: Astah

Original string: astah
Translation: astahay

Output 3:

Please enter your string.
String: astah,

Original string: astah,
Translation: astah,ay

Output 4:

Please enter your string.
String: Astah la vista, baby.

Original string: astah la vista, baby.
Translation: astah la vista, baby.ay

Upvotes: 0

Views: 1986

Answers (1)

Shadow
Shadow

Reputation: 4006

You can split your input sentence into individual words using the String.split function. That's the first step, something like this:

String[] words = originalString.split(" ");
String[] pigWords;

Once you've done that, you can loop over each word to change it:

pigWords = new String[words.length];
for (int i = 0; i < words.length; i++)
    pigWords[i] = pigLatinator(words[i])

Then, modify your pigLatinator to check if the end of the word has a punctuation mark, and insert "ay" before that punctuation mark:

int index = originalString.length() -1;
char lastChar = originalString.charAt(index);
if (char == "." || char == ",")
    String pigString = originalString.substring(0,index) + "ay" + originalString.charAt(index);

Since your PigLatin class is a separate class to where your main is, you should be able to do something like this:

class PigLatin
{
    String originalString = "", pigString = "";

    public void input() {
        System.out.println("Please enter your string.");
        System.out.print("String: ");
        originalString = keyboard.nextLine().toLowerCase();
    }

    private String pigLatinator(String word)
    {
        String output = "";
        int index = word.length() -1;
        char lastChar = word.charAt(index);

        if(word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u"))
        {
            if (char == '.' || char == ',' || char == '!')
                output = word.substring(0,index) + "ay" + word.charAt(index);
            else
                output = word + "ay";
        }
        else
        {
            if (char == '.' || char == ',' || char == '!')
                output = word.substring(1,index) + word.charAt(0) + "ay" + word.charAt(index);
            else
                output = word.substring(1) + word.charAt(0) + "ay";
        }

        return output;
    }

    public void translate()
    {
        String[] words = originalString.split(" ");
        for (int i = 0; i < words.length; i++)
        {
            pigString += pigLatinator(words[i]) + " ";
        }
    }

    public void display()
    {
        System.out.println();
        System.out.println("Original string: " +originalString);
        System.out.println("Translation: " +pigString);
    }
}

Then, inside your main function, declare a new PigLatin object, and call the appropriate functions:

public static void main(String[] args)
{
    PigLatin myPigLatin = new PigLatin();
    myPigLatin.input();
    myPigLatin.translate();
    myPigLatin.display();
}

This code is untested, but it should work, it's simple enough.

Upvotes: 1

Related Questions