Results 1 to 4 of 4
  1. #1
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Numbers in Pig Latin Code

    Greetings all,
    So I have my Pig Latin code complete, but I am trying to get it to ignore numbers, but I am not sure what direction to take here:

    When I run the code now and use a sentence with a number in it, I get the following:

    Input your sentence for conversion to Pig Latin: You are the 30th Person to "Visit" today
    Your sentence is - You are the 30th Person to "Visit" today
    Your converted sentence is = Ouyay areway ethay 30thway Ersonpay otay "Isitvay" odaytay

    It should be: Ouyay areway ethay 30th Ersonpay otay "Isitvay" odaytay

    Any guidance would be appreciated. My Code is below:

    Java Code:
    package pig.latin.string;
    
    import java.util.*;
    import java.util.regex.Pattern;
    
    public class pigLatinString {
    
        public static void main(String[] args) {
    
    
            // Initialized Variables
            String startSentence;
            String pigLatinSentence = "";
            int countToken = 0;
            String start;
            String end;
            char charHolder;
            int punctuation = 0;
            Scanner keyboard = new Scanner(System.in);
    
            // Will take user string and print it out to show initial state
            System.out.print("Input your sentence for conversion to Pig Latin: ");
            startSentence = keyboard.nextLine(); // Add the name input into the name variable
            System.out.println("Your sentence is - " + startSentence);
    
            StringTokenizer parseSentence = new StringTokenizer(startSentence, ",.:;()[]{}\"!@#$%^&* ", true);
            int tokenCount = parseSentence.countTokens();
    
            //Create array to hold tokenized values inclusive of puncuation
            String[] breakSentence = new String[tokenCount];
    
            //Populate array breakSentence to hold the tokenized values for use
            while (parseSentence.hasMoreTokens()) {
                breakSentence[countToken] = parseSentence.nextToken();
                countToken++;
            }
    
    
    
            for (int ctr = 0; ctr < tokenCount; ctr++) {
                boolean checkCase = Character.isUpperCase(breakSentence[ctr].charAt(0));
    
    
                punctuation = 0;
                int value = firstVowelLoc(breakSentence[ctr]);
                if (Pattern.matches("[,.:;(){}\"'!@#$%^&* ]", breakSentence[ctr])) {
                    punctuation = 1;
                }
    
                if (value == 0 && punctuation < 1) {
                    breakSentence[ctr] += "WAY";
                    breakSentence[ctr] = breakSentence[ctr].toLowerCase();
                    if (checkCase == true) {
                        String firstChar = breakSentence[ctr].substring(0, 1).toUpperCase();
                        String lastPart = breakSentence[ctr].substring(1);
                        breakSentence[ctr] = firstChar + lastPart;
                    }
                } else if (value == 0 && punctuation == 1) {
    
                    breakSentence[ctr] = breakSentence[ctr];
                } else {
    
                    start = breakSentence[ctr].substring(0, value);
                    end = breakSentence[ctr].substring(value);
                    breakSentence[ctr] = end + start + "AY";
                    breakSentence[ctr] = breakSentence[ctr].toLowerCase();
                }
                if (checkCase == true) {
                    String firstChar = breakSentence[ctr].substring(0, 1).toUpperCase();
                    String lastPart = breakSentence[ctr].substring(1);
                    breakSentence[ctr] = firstChar + lastPart;
                }
    
    
    
            }
    
            for (int finCount = 0; finCount < tokenCount; finCount++) {
                pigLatinSentence += breakSentence[finCount];
            }
    
            System.out.println("Your converted sentence is = "+pigLatinSentence);
    
        } //public static void main(String[] args)
    
        public static int firstVowelLoc(String word) {
            for (int i = 0; i < word.length(); i++) {
                if (word.charAt(i) == 'a' || word.charAt(i) == 'e'
                        || word.charAt(i) == 'i' || word.charAt(i) == 'o'
                        || word.charAt(i) == 'u' || word.charAt(i) == 'A'
                        || word.charAt(i) == 'E' || word.charAt(i) == 'I'
                        || word.charAt(i) == 'O' || word.charAt(i) == 'U') {
                    return i;
                }
            }
            return 0;
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,000
    Rep Power
    10

    Default Re: Numbers in Pig Latin Code

    Take a look at the Pattern class for useful functions.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    wfsteadman is offline Member
    Join Date
    Jan 2013
    Location
    Texas
    Posts
    45
    Rep Power
    0

    Default Re: Numbers in Pig Latin Code

    I ended up adding some code to the firstVowelLoc class:

    Java Code:
    public static int firstVowelLoc(String word)
      {
        for (int i = 0; i < word.length(); i++)
        {
          if (word.charAt(i) == 'a' || word.charAt(i) == 'e'
                  || word.charAt(i) == 'i' || word.charAt(i) == 'o'
                  || word.charAt(i) == 'u' || word.charAt(i) == 'A'
                  || word.charAt(i) == 'E' || word.charAt(i) == 'I'
                  || word.charAt(i) == 'O' || word.charAt(i) == 'U')
          {
            return i;
          }
          
          if(word.charAt(0) == '0' || word.charAt(0) == '1' || word.charAt(0) == '2' || word.charAt(0) == '3' || word.charAt(0) == '4' || word.charAt(0) == '5' || word.charAt(0) == '6'
          || word.charAt(0) == '7' || word.charAt(0) == '8' || word.charAt(0) == '9')
        {
              return -1;
        }
          
        }
        return 0;
      }
    Then in the body of the code I checked for -1 and if that was the case, I just ran:

    Java Code:
    else if (value == -1)
          {
            breakSentence[ctr] = breakSentence[ctr];
          }
    Seems to work. I am sure my code can be more elegant. I planning on splitting it out into separate classes as well. Just haven't gotten there yet.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,000
    Rep Power
    10

    Default Re: Numbers in Pig Latin Code

    That's one way to do it, but you could have reduced that 10-element if statement down to a single boolean expression using the Pattern class. For that matter, you could have used the methods in the Character class as well. But as long as it works, go with it.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Putting array of numbers in SQL (WHERE) code
    By iNko in forum New To Java
    Replies: 10
    Last Post: 12-10-2012, 04:35 PM
  2. Pig Latin translator
    By zygmfb in forum New To Java
    Replies: 4
    Last Post: 10-18-2012, 07:20 AM
  3. Code Tag Line Numbers
    By awinston in forum Suggestions & Feedback
    Replies: 3
    Last Post: 08-16-2012, 05:22 AM
  4. english to pig latin, I/O in gui
    By MaceMan in forum New To Java
    Replies: 18
    Last Post: 11-13-2011, 05:53 AM
  5. Pig Latin Translator
    By danthegreat in forum New To Java
    Replies: 22
    Last Post: 09-11-2011, 12:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •