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();
    p.pigConvert(t);
   }
   p.pigReport();
  }
}

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," ?!.,");
    while(word.hasMoreTokens()){
      result += convertWord(word.nextToken());
      result += " ";
    }
    System.out.println(result);
  }
  //this method translates a sentence into piglatin
  
  private String convertWord(String word){
    String result = "";
    for (int i = 0; i < word.length(); i++){
      if(startVowel(word.charAt(i))){
      result = word + "ay";
    }
    else{
      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(){
    System.out.println();
  }
}
  //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

Recommended Answers

All 3 Replies

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.

@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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.