Hey guys, I need a little help on PigLatin translator. This is "driver" class I'm given that I cannot change

import java.util.*;
public class PigDriver{
  public static void main(String[] args){
   Scanner scan = new Scanner(System.in);
   String t = " ";
   Piglatin p = new Piglatin();
   while(t.length() > 0){
    t = scan.nextLine();
    t = t.toLowerCase();

And this is my body to my other Piglatin class except when I test my code, it works but only sometimes. Letters like "b, g, z" sometimes dont work and the "aeiou" doesnt always work either. Can anyone help?

Here's my code that can be edited/changed

import java.util.*;

public class Piglatin{
  public void pigConvert(String t){
    String result = "";
    StringTokenizer word = new StringTokenizer(t," ?!.,");
      result += convertWord(word.nextToken());
      result += " ";
  //this method translates a sentence into piglatin
  private String convertWord(String word){
    String result = "";
    for (int i = 0; i < word.length(); i++){
      result = word + "ay";
      result = word.substring(1) + word.charAt(0) + "ay";
    return result;
  //this method converts a word into piglatin
  private boolean startVowel(char x){
    return x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u';
  //this method checks to see if the word begins with a vowel
  public void pigReport(){
  //this method prints out the input in pig latin

EDIT: Here are some example outputs
Input: "how is this string of words working?"
Output: "owhay siay histay tringsay foay ordsway orkingway"

Input: a bear can do everything from growling happy inside jokingly to knocking little men now
output: aay earbay ancay doay verythingeay romfay rowlinggay appyhay insideay okinglyjay toay nockingkay littleay enmay ownay

Edited by fengapapitt: n/a

7 Years
Discussion Span
Last Post by peter_budo

You need to decide which version of Pig Latin you want to do...
1) Simple just check first character trash becomes rashtay
2) Complex where till you hit vowel you have to move preceding characters at end like trash yields ash-tray and plunder yields under-play


For the most part I'll keep my info brief.

I took out the over initialization of result within your program.
I created a private string instead.

That might be the problem you are experiencing.
I'll post code soon.

Edited by Joejackey: n/a


@Joejackey you can save it for another student that will come back in couple of months asking same question. As you see original poster never came back with any questions or results.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.