Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Piglatin program that prints no words out:(

  1. #1
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Piglatin program that prints no words out:(

    I am a high school student in a beginners programming class and I have to create a pig latin program that takes the first consonants and puts them to the end of the word and ads "ay". I really don't know what I'm doing and my code is printing out no words and I've tried to fix it and I don't know what the issue is because no error codes are showing up. I can't use arrays and can only use for loops, if/else, and while.


    import java.util.Scanner;
    public class PigLatin
    {
       public static void main(String args[])
       {
           Scanner input = new Scanner(System.in);
           System.out.println("Enter a phrase: ");
           String phrase = input.nextLine();
           System.out.println("Here is your phrase in pig latin: " + sepword(phrase));
       }
       public static String sepword(String phrase)
       {
           phrase = phrase + " ";
           String sentence = "";
           for (int i = 0; i < phrase.length(); i++)
           {
               String word = "";
               if (phrase.charAt(i) == ' ')
               {
                  word = phrase.substring(0, i);
                  if (word.charAt(0) == 'a' ||
                      word.charAt(0) == 'i' ||
                      word.charAt(0) == 'o' ||
                      word.charAt(0) == 'u' ||
                      word.charAt(0) == 'e')
                      {
                          sentence+=vowel(word);
                      }
                   else
                   {
                       sentence+=consonant(word);
                   }
               }
               phrase = phrase.substring(i+1);
           }
           return sentence;
       }
       public static String vowel(String word)
       {
           return word+"-ay ";
       }
       public static String consonant(String word)
       {
           for (int i = 0; i < word.length(); i++)
           {
               String firstletters = "";
               while (word.charAt(i) != 'a' ||
                      word.charAt(i) != 'i' ||
                      word.charAt(i) != 'o' ||
                      word.charAt(i) != 'u' ||
                      word.charAt(i) != 'e')
                      {
                         firstletters+=word.charAt(i);
                      }
                      word = word.substring(i)+"-"+firstletters+"ay ";
           }
           return word;
       }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Piglatin program that prints no words out:(

    Can you add some comments to the code describing what each method is supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Piglatin program that prints no words out:(

    import java.util.Scanner;
    public class PigLatin_Rider
    {
       public static void main(String args[])
       {
           Scanner input = new Scanner(System.in);
           System.out.println("Enter a phrase: ");
           String phrase = input.nextLine();
           String piglatin = sepword(phrase);
           System.out.println("Here is your phrase in pig latin: " + piglatin);
       }
       public static String sepword(String phrase) //separate words and then determine whether they begin with vowels or consonants
       {
           phrase = phrase + " "; //add a space to original phrase to catch the last word
           String sentence = ""; 
           for (int i = 0; i < phrase.length(); i++)
           {
               String word = "";
               if (phrase.charAt(i) == ' ')
               {
                  word = phrase.substring(0, i); 
                  if (word.charAt(0) == 'a' ||  //check if the word begins with a vowel; if it does, call on vowel method
                      word.charAt(0) == 'i' ||
                      word.charAt(0) == 'o' ||
                      word.charAt(0) == 'u' ||
                      word.charAt(0) == 'e')
                      {
                          sentence+=vowel(word);
                      }
                   else //if word doesn't begin with vowel, call on consonant method
                   {
                       sentence+=consonant(word);
                   }
               }
               phrase = phrase.substring(i+1); //reset phrase to get rid of the word I just "piglatin"d
           }
           return sentence;
       }
       public static String vowel(String word) //adds "ay" to the end of a word that starts with vowel
       {
           return word+"-ay ";
       }
       public static String consonant(String word)
       {
           for (int i = 0; i < word.length(); i++)
           {
               String firstletters = ""; 
               while (word.charAt(i) != 'a' ||  //goes through while loop until it finds a consonant, then adds the string of vowels after the consonant
                      word.charAt(i) != 'i' ||
                      word.charAt(i) != 'o' ||
                      word.charAt(i) != 'u' ||
                      word.charAt(i) != 'e')
                      {
                         firstletters+=word.charAt(i);
                      }
                      word = word.substring(i)+"-"+firstletters+"ay ";
           }
           return word;
       }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Piglatin program that prints no words out:(

    The comments are too thin. For example:
    public static String sepword(String phrase) //separate words and then determine whether they begin with vowels or consonants
    What is in the String that the method returns?
    How does the code build the String that is returned?
    What values are in the variables: word, phrase and sentence?
    How are those variables used to create the String that is to be returned?

    What does this method do?
    public static String consonant(String word)

    Note: method names should be verbs that describe what action they take. Variable names describe the data they hold.
    consonant is a noun. It does not describe the action the method will take.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Piglatin program that prints no words out:(

    How are you trying to debug the code? Add some print statements in all the methods that print out the values they receive and return.
    Also print out the values of the variables used in the loop in sepword so you can see what the computer sees when the code is executed.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. system.out prints twice in a small program
    By vinaysagar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 12th, 2014, 07:36 AM
  2. Replies: 11
    Last Post: March 10th, 2013, 07:40 PM
  3. when i run the program to find the y values it always prints y2 instead
    By jamesR in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 30th, 2012, 08:38 AM
  4. [SOLVED] PigLatin Program Help
    By Webby in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 6th, 2011, 09:13 PM
  5. A program that reads in secounds, and prints out hours minutes
    By CYKO in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2009, 10:42 PM

Tags for this Thread