1 Replies - 467 Views - Last Post: 10 March 2013 - 12:21 PM Rate Topic: -----

#1 kulafokiller  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-March 13

Help with Pig Latin Translator

Posted 10 March 2013 - 01:28 AM

so this is a piglatin translator
the rule are: when the word starts in vowel letter it adds "way" if it starts in consonant it finds the first vowel letter and the letters before the vowel will be transfered in the end part of the word and add "ay"

the problem is when the word starts with consonant it translates but then the letter/s before the vowels isn't transferred in the end
ex:
inputted word: kulafo
output: ulafo+ay

here is the code:

import java.util.StringTokenizer;
import java.io.*;
public class kulafokiller {
    private static char[] vowels ={'a', 'e', 'i', 'o', 'u'};
    private static StringTokenizer tokenizer;
    private static String word;
    public static void getString()
    {
        BufferedReader kulafo = new BufferedReader(new InputStreamReader(System.in));
      
        System.out.println("Enter text to be translated");
       try { String text = kulafo.readLine();
       tokenizer = new StringTokenizer(text);
       } catch (IOException e) {
        System.out.println(e); }
        while(tokenizer.hasMoreTokens())
        {
            word = tokenizer.nextToken();
            System.out.println(shifter(word));
        }
    }
    public static String shifter(String text)
    {
    //  text = tokenizer.nextToken();
        int length = text.length();
        for(int x = 0; x < vowels.length; x++)
        {
            if(text.charAt(0) == vowels[x])
            {
                System.out.println("");
                text+="way";
                return text;
            }
        }
        //System.out.println("flag1");
        text += text.charAt(0);
        //System.out.println("0 pos is" + text.charAt(0));
        text = text.substring(0,length);
        text+="ay";
        return text;
    }
    public static void main(String[] args)
    {
        getString();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help with Pig Latin Translator

#2 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-February 13

Re: Help with Pig Latin Translator

Posted 10 March 2013 - 12:21 PM

I think you want to look at the api for substring

You need to change the parameters for text.substring(0, length) to text.substring(1, text.length()). If you use the length variable from your code you cutoff the ending letter that you just added on the end. And by using 0 instead of 1 you're not cutting off the first letter of the word.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1