# Learning Java in 2020 – Beginner – Pig Latin

Overview reference

Python Solution

## Problem

### Goal

Simply take a word, check if it begins with a vowel or a consonant cluster and append “ay” accordingly. If the word starts with a vowel, add an “ay” to the end of the word. Else extract the consonant cluster from the word, append “ay” to the cluster and join the remaining part of the word with the “ay”ed consonant cluster. ( Maybe I should have explained with actual words 😀 )

### Pseudo-code

• Get user input
• Check if the first character is a vowel or consonant
• If vowel, print out user input + “ay”
• If consonant, find get consonant cluster ( group of consonant before the first vowel in the user input)
• Print out part of user input starting from the first vowel + consonant cluster + “ay”

## Solution

### Determine if the first character of the word is a vowel

```private static boolean firstCharisVowel(char firstcharacter) {

boolean isvowel = false;
String vowels = "aeiou";
for(int i = 0; i < vowels.length(); i++) {
char character = vowels.charAt(i);
if(character == firstcharacter) {
isvowel = true;
break;
}
}

return isvowel;
}```

This private static method returns true if the given character is a vowel or false if not. Based on my brief research, there are other ways of searching if a value is present in a list. So instead of looping through the String vowels and checking the firstcharacter argument against each character in the String, I could have used the String method called indexOf to find the first occurrence of the given character in the String. This method would either return a number => 0 if such character existed in the String or a -1 if absent. Just so you know of alternatives 😀

### Extract consonant cluster before first vowel occurence in string

```private static String getConsonantCluster(String word) {
String consonantcluster = "";
for(int i = 0; i < word.length(); i++) {
char current_char = word.charAt(i);
if(PigLatin.firstCharisVowel(current_char)) {
break;
}else {
consonantcluster += current_char;
}
}
return consonantcluster;
}```

Unlike the first method, I really could not come up with a simpler alternative for isolation of consonant clusters. I am interested in hearing of your own solutions. Here I loop through given word, append each consonant and break the loop, once the current character is a vowel. The function returns the consonant cluster.

### Transform given word to piglatin

```private static String wordToLatin(String word) {

String latin = "";
boolean isvowel  = PigLatin.firstCharisVowel(word.charAt(0));
if(isvowel) {
latin += word + "ay";
}
else {
String consonantcluster = PigLatin.getConsonantCluster(word);
latin += word.substring(word.indexOf(consonantcluster) +  consonantcluster.length()) + consonantcluster + "ay";
}
return latin;
}
```

The code snippet above is almost a direct representation of the provided pseudo-code.

## Trigger Action

```public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length == 0) {
System.out.println("Provide a word or a sentence an press Enter");
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String piglatinized = PigLatin.wordToLatin(text);
System.out.println("Your Pig Latin is : " + piglatinized);
}else {
for(String word : args) {
String piglatin = PigLatin.wordToLatin(word);
System.out.println("Word: " + word + " ---- " + piglatin);
}
}

}
```

I thought the program would be a bit inflexible if it was strictly dependent on the user typing in the words AFTER the program was triggered. Since we can access command line arguments from the main method, why not use it? Here, I check if the user typed in any words apart from “java [PROGRAM].java” in the command line. If empty, simply ask the user for input, else use the words he/she already typed in.

## Run Program

Based on the main function above, there are two ways of providing input.

```C:<path>\com.learnjava\src\com\learnjava\beginner>java PigLatin.java
Provide a word or a sentence an press Enter
hello
Your Pig Latin is : ellohay```
```C:<path>\com.learnjava\src\com\learnjava\beginner>java PigLatin.java smile string stupid glove trash floor store
Word: smile ---- ilesmay
Word: string ---- ingstray
Word: stupid ---- upidstay
Word: glove ---- oveglay
Word: trash ---- ashtray
Word: floor ---- oorflay
Word: store ---- orestay```

## PigLatin.java

```package com.learnjava.beginner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class PigLatin {

public static void main(String[] args) {
// TODO Auto-generated method stub
if(args.length == 0) {
System.out.println("Provide a word or a sentence an press Enter");
Scanner sc = new Scanner(System.in);
String text = sc.nextLine();
String piglatinized = PigLatin.wordToLatin(text);
System.out.println("Your Pig Latin is : " + piglatinized);
}else {
for(String word : args) {
String piglatin = PigLatin.wordToLatin(word);
System.out.println("Word: " + word + " ---- " + piglatin);
}
}

}

private static String wordToLatin(String word) {

String latin = "";
boolean isvowel  = PigLatin.firstCharisVowel(word.charAt(0));
if(isvowel) {
latin += word + "ay";
}
else {
String consonantcluster = PigLatin.getConsonantCluster(word);
latin += word.substring(word.indexOf(consonantcluster) +  consonantcluster.length()) + consonantcluster + "ay";
}
return latin;
}
private static String getConsonantCluster(String word) {
String consonantcluster = "";
for(int i = 0; i < word.length(); i++) {
char current_char = word.charAt(i);
if(PigLatin.firstCharisVowel(current_char)) {
break;
}else {
consonantcluster += current_char;
}
}
return consonantcluster;
}
private static boolean firstCharisVowel(char firstcharacter) {

boolean isvowel = false;
String vowels = "aeiou";
for(int i = 0; i < vowels.length(); i++) {
char character = vowels.charAt(i);
if(character == firstcharacter) {
isvowel = true;
break;
}
}

return isvowel;
}
}
```

## Question

How can you prevent the user from inputting an empty string ?