Skip to content
Sign upLog in
← Back to Community
Pig Latin translator Java
Profile icon

I created a pig latin translator in java!

The original project is here and it took a while to make I can't do piggy[0] and I had the from pig latin code not working it also can figure out if it is in pig latin or not this is how it works

piggy = sc.nextLine().split(" ");

splits the words and adds them to the piggy array

for (int i = 0; i < pigSize; ++i) { piggy[i] = FeedPig(piggy[i]); }

I made a variable for the length in the piggy array and now it cycles threw the words and converts them so if we're on the 2nd word word it feeds it to the feedpig of the word and converts it

public static String FeedPig (String piggy) { ay = piggy.substring(piggy.length() - 2); if (!ay.equals("ay")) { String oinkpig = piggy.substring(1) + piggy.charAt(0) + "ay"; return oinkpig; } else if (ay.equals("ay")) { piggy = piggy.substring(0,piggy.length() - 2); String firstLetter = piggy.substring(piggy.length() - 1); piggy = piggy.substring(0,piggy.length() - 1); String hog = firstLetter + piggy; return hog; } return null; }

the first part determents if it's in pig latin or not then it converts it, and since FeedPig is a string it needs to return values, and then

System.out.println("Result: " + String.join(" ", piggy));

prints the returned result


Profile icon
Profile icon
Profile icon
Profile icon