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: PigLatinTranslator produces no output

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default PigLatinTranslator produces no output

    This program is supposed to read a sentence of user input and translate it to piglatin by appending "yay" to a word beginning with a vowell and for words beginning with consonants or consonant blends ("br", "tw", "st" etc..) moving the consonant/blend to the end then adding an "ay". My version of the program does not produce any output. But it does prompt me to type in a sentence (which is in the do loop of my main method). Any help, please

    main:
    //PigLatin.java
     
    import java.util.Scanner;
     
    public class PigLatin
    {
      public static void main (String[] args)
      {
        String sentence, result, another;
     
        Scanner scan = new Scanner (System.in);
     
        do
        {
          System.out.println ();
          System.out.println ("Enter a sentence (no punctuation):");
          sentence = scan.nextLine();
     
          System.out.println ();
          result = PigLatinTranslator.translate (sentence);
          System.out.println ("That sentence in Pig Latin is:");
          System.out.println (result);
     
          System.out.println();
          System.out.print ("Translate another sentence (y/n)?");
          another = scan.nextLine();
        }
        while (another.equalsIgnoreCase("y"));
      }
    }

    And the called method:
    //PigLatinTranslator.java
     
    import java.util.Scanner;
     
    public class PigLatinTranslator
    {
      //translates a sentence of words into piglatin
      public static String translate (String sentence)
      {
        String result = "";
     
        sentence = sentence.toLowerCase();
     
        Scanner scan = new Scanner (sentence);
     
        while (scan.hasNext());
        {
          result += translateWord (scan.next());
          result += " ";
        }
     
      return result;
      }
     
      //translates ONE word into piglatin. Using the language rules of
      //that language.
      private static String translateWord (String word)
      {
        String result = "";
     
        if (beginsWithVowel (word))
            result = word + "yay";
        else
            if (beginsWithBlend (word))
                result = word.substring(2) + word.substring(0,2) + "ay";
            else
                result = word.substring(1) + word.charAt(0) + "ay";
     
        return result;
      }
     
      //Finds out if the word begins with a vowell
      private static boolean beginsWithVowel (String word)
      {
        String vowels = "aeiou";
     
        char letter = word.charAt(0);
     
        return (vowels.indexOf (letter) != -1);
      }
     
      //Finds out if the word begins with any of the two character
      //consonant blends
      private static boolean beginsWithBlend (String word)
      {
        return ( word.startsWith ("bl") || word.startsWith ("sc") || 
                 word.startsWith ("br") || word.startsWith ("sh") || 
                 word.startsWith ("ch") || word.startsWith ("sk") || 
                 word.startsWith ("cl") || word.startsWith ("sl") ||
                 word.startsWith ("cr") || word.startsWith ("sn") ||
                 word.startsWith ("dr") || word.startsWith ("sm") ||
                 word.startsWith ("dw") || word.startsWith ("sp") ||
                 word.startsWith ("fl") || word.startsWith ("sq") ||
                 word.startsWith ("fr") || word.startsWith ("st") ||
                 word.startsWith ("gl") || word.startsWith ("sw") ||
                 word.startsWith ("gr") || word.startsWith ("th") ||
                 word.startsWith ("kl") || word.startsWith ("tr") ||
                 word.startsWith ("ph") || word.startsWith ("tw") ||
                 word.startsWith ("pl") || word.startsWith ("wh") ||
                 word.startsWith ("pr") || word.startsWith ("wr") );
      }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,138
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: PigLatinTranslator produces no output

    How have you tried debugging the code to see what it is doing?
    Try adding some calls to the println() method that prints out the value of all variables as their value is changed.
    Also print out the value returned by all methods so you can see what the program is doing.
    For example what does the beginsWithBlend() method return for the String passed to it.

    --- Update ---

    One potential problem I see is that there are several blocks of code inside if statements that are not enclosed in {}s. Using {}s will ensure that the placement of blocks of code will be where you want it to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    craigjlner (September 26th, 2013)

  4. #3
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: PigLatinTranslator produces no output

    Hi Norm. Well after much experimenting I changed to translate(sentence); in the main method to translateWord(sentence); sort of bypassing the translate method entirely. I changed translateWord to public and ran it and now if works finally yaay! But because .toLowerCase(); is in the bypassed method it all prints out with the capitals now and it doesnt process a sentence one word at a time, just considers it to be one whole word. Heres an example of what its doing:

    craig@craig-laptop:~/Documents/panda/java3$ java PigLatin
     
    Enter a sentence (no punctuation):
    No I wont enter a sentence
     
    That sentence in Pig Latin is:
    o I wont enter a sentenceNay
     
    Translate another sentence (y/n)?y
     
    Enter a sentence (no punctuation):
    Oh fine I'll keep entering sentences then.
     
    That sentence in Pig Latin is:
    h fine I'll keep entering sentences then.Oay
     
    Translate another sentence (y/n)?

    I'm going to keep trying this one, but if you have a tip for me that would be great..

  5. #4
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: PigLatinTranslator produces no output

    The reason for this is because the method you bypassed doesn't just set the sentence to lower case letters it also does something else. Do as Norm mentioned and put some System.out.println() calls throughout your methods. In this manner you can narrow down where the code is doing behavior you don't want.

  6. The Following User Says Thank You to Ubiquitous For This Useful Post:

    craigjlner (September 26th, 2013)

  7. #5
    Member
    Join Date
    Mar 2013
    Posts
    33
    My Mood
    Innocent
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: PigLatinTranslator produces no output

    craig@craig-laptop:~$ cd Documents/panda/java3
    craig@craig-laptop:~/Documents/panda/java3$ javac PigLatinTranslator.java
    craig@craig-laptop:~/Documents/panda/java3$ javac PigLatin.java
    craig@craig-laptop:~/Documents/panda/java3$ java PigLatin
     
    Enter a sentence (no punctuation):
    Ok then fine I will enter a sentence
     
    That sentence in Pig Latin is:
    okyay enthay inefay iyay illway enteryay ayay entencesay 
    okyay enthay inefay iyay illway enteryay ayay entencesay yay
    false
    true
     
    Translate another sentence (y/n)?n
    craig@craig-laptop:~/Documents/panda/java3$

    Thanks for the help guys. I have found there was a semicolon here in my code: while (scan.hasNext()); which I got rid of and it worked. Debugging tip really worked well I will be using that alot now
    //PigLatin.java
     
    import java.util.Scanner;
     
    public class PigLatin
    {
      public static void main (String[] args)
      {
        String sentence, result, another;
     
        Scanner scan = new Scanner (System.in);
     
        do
        {
          System.out.println ();
          System.out.println ("Enter a sentence (no punctuation):");
          sentence = scan.nextLine();
     
          System.out.println ();
          result = PigLatinTranslator.translate (sentence);
          System.out.println ("That sentence in Pig Latin is:");
          System.out.println (result);
     
          System.out.println (PigLatinTranslator.translateWord (result));
          System.out.println (PigLatinTranslator.beginsWithBlend (result));
          System.out.println (PigLatinTranslator.beginsWithVowel (result));
     
          System.out.println();
          System.out.print ("Translate another sentence (y/n)?");
          another = scan.nextLine();
        }
        while (another.equalsIgnoreCase("y"));
     
      }
    }

Similar Threads

  1. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  2. reading from text file produces extra s p a c e s, even on numbers 1 0 1
    By Spidey1980 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 3rd, 2011, 09:18 PM
  3. Why am I getting this output?
    By ptabatt in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 6th, 2010, 07:36 PM
  4. need help with output.
    By VictorCampudoni in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 24th, 2010, 11:25 PM
  5. OutPut.
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: August 29th, 2009, 10:54 AM