Help with a program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help with a program

Hybrid View

  1. #1
    Join Date
    Dec 2005
    Posts
    27

    Help with a program

    Hello everyone i am having trouble with this program that i am writing. The purpose of this program is to convert a string entered by a user into pig latin.
    The rules of Pig Latin are,
    No vowels in the English word, then the Pig Latin word is just the English word + "ay"

    If the English word begins with a vowel, then the Pig Latin word is just the English word + "yay".

    Otherwise (if the English word has a vowel in it and yet doesn't start with a vowel), then the Pig Latin word is end + start + "ay

    When i run this program and i test phrases like "I green" i get "Iyay greenay".
    here is my program
    Code:
    /**
     * This program translate a string into Pig Latin
     */
    import java.util.*; 
    import java.io.*; 
    public class Piglatinator
    {
        public static void main(String []args) throws IOException 
        {
            System.out.println("This program translate a string into Pig Latin"); 
            System.out.println("To end this program at any time just enter \"q\""); 
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
            String str = ""; 
            do
            {
                System.out.println("\nPlease enter a string"); 
                str = in.readLine();
                System.out.print(translateToPiglatin(str)); 
            }
            while (!str.equalsIgnoreCase("q") && !str.equalsIgnoreCase("quit")); 
    
        }
        public static String translateToPiglatin(String str) throws IOException 
        {
            StringTokenizer st = new StringTokenizer(str);
            int num = st.countTokens(); 
            String [] word = new String [num]; 
            int counter = 0; 
            String s = ""; 
            while (st.hasMoreTokens()) 
            {
                word [counter] = st.nextToken();
                if(calculateVowelPositions(word [counter]) == 0)
                {
                    word[counter] = word[counter].concat("yay"); 
                }
                else if(word[counter].charAt(0) == 'a' || word[counter].charAt(0) == 'A' || word[counter].charAt(0) == 'e' || word[counter].charAt(0) == 'E' || word[counter].charAt(0) == 'i' || word[counter].charAt(0) == 'I' || word[counter].charAt(0) == 'o' || word[counter].charAt(0) == 'O' || word[counter].charAt(0) == 'u' || word[counter].charAt(0) == 'U')
                {
                    word[counter] = str.toLowerCase(); 
                    word[counter] = str.concat("ay"); 
                }
                else
                {      
                    int vowel = calculateVowelPositions(str); 
                    String end = word[counter].substring(0, vowel); 
                    String front = word[counter].substring(vowel , word[counter].length());
                    front = front + end + "ay";
                    word [counter] = front;
                }
                s = s.concat(word [counter] + " "); 
                counter ++; 
            }
            return s;       
        }
        public static int calculateVowelPositions(String str) 
        {
            str = str.toLowerCase(); 
            str = str.trim(); 
            int position = 0; 
            for(int j = 0; j < str.length(); j ++)
            {
                if(str.charAt(j) == 'a')
                {
                    position = j;
                    break; 
                }
                else if (str.charAt(j) == 'e')
                {
                    position = j;
                    break;    
                }
                else if (str.charAt(j) == 'i')
                {
                    position = j;
                    break; 
                }
                else if(str.charAt(j) == 'o')
                {
                    position = j;
                    break; 
                }
                else if(str.charAt(j) == 'u')
                {
                    position = j;
                    break; 
                }
            }
            return position; 
        }
    }

  2. #2
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Change:
    Code:
    int vowel = calculateVowelPositions(str);
    to:
    Code:
    int vowel = calculateVowelPositions(word[counter]);
    You had it only checking the first vowel of the whole String instead of a specific word.
    Last edited by destin; 01-09-2006 at 09:57 PM.

  3. #3
    Join Date
    Dec 2005
    Location
    New Jersey
    Posts
    290
    Btw, there are a few things you left out. For example, type in "quack". You'll get "uackqay". Another thing (you might run into some trouble with this), it's not just vowels, it's vowel sounds. hour become houryay, honest becomes honestyay, etc.

Similar Threads

  1. problem in program in c++
    By mheasen in forum Architecture and Design
    Replies: 0
    Last Post: 03-20-2002, 09:24 AM
  2. Please help with running executable vb program.
    By Gordon Reichhardt in forum VB Classic
    Replies: 2
    Last Post: 01-08-2002, 10:06 AM
  3. How can I build copy protection into my program
    By W.Pierce in forum VB Classic
    Replies: 1
    Last Post: 12-11-2001, 08:28 AM
  4. The invisible program
    By James Abbott in forum VB Classic
    Replies: 2
    Last Post: 11-24-2001, 05:04 AM
  5. Replies: 0
    Last Post: 12-15-2000, 10:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center