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 4 of 4

Thread: infinite loop on pig latin program

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default infinite loop on pig latin program

    I'm new to programming (this is my 3rd program ever)
    We have an assignment to create a Pig Latin translator, but when i run it it repeats the answer over and over again. Not sure what I am doing wrong.


     
    /******************
    *Pig Latin Program
     
    Eddy Caraballo
    ******************/
     
    import java.util.Scanner;
    public class PigLatin {
     
       public static void main(String[] args)
       {
          System.out.println("give me a word to translate or press 'q' to quit");    //displaying question//
          Scanner scan = new Scanner(System.in);       
     
     
          String wordToTranslate = scan.nextLine();                                  //taking user input and creates new variable//
          char v = Character.toLowerCase(wordToTranslate.charAt(0));                 //looking for the first letter of the word that is being input//
     
          while(!wordToTranslate.equalsIgnoreCase("q"))                              //should loop until "q" is entered//
          {
     
          if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u')              //if first letter of inputted word is any of these letters(vowels)//
          {
              String pigLatinTranslation = wordToTranslate + "way";                  //then create new word with way at the end//
              System.out.println(pigLatinTranslation);                               //displays new word//
          }
          else                                                                       //otherwise//
          {
             String firstLetter = wordToTranslate.substring(0,1);                    //creates a new variable named firstLetter that comes from a section(substring) of "wordToTranslate" starting at character 0 and ending at character 1//
             String endOfWord = wordToTranslate.substring(1,wordToTranslate.length()); //creates a new variable named endOfWord that comes from a section(substring) of "wordToTranslate" starting at character 1 and ending at the length of the word//
     
             System.out.println (endOfWord + firstLetter + "ay");
          }
     
     
          }
     
     
       }  
     
    }

    Thanks for any advice
    Last edited by Heri623; June 8th, 2014 at 02:44 PM. Reason: correct dispaly


  2. #2
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    373
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default Re: infinite loop on pig latin program

    Well, just think about it, your loop will stop when the following condition is false:
    wordToTranslate.equalsIgnoreCase("q")
    Now look at every single point in your program where the value of "wordToTranslate" is being set.
    You will realize that it is only set once, and that is at the very beginning of the program.
    So if the condition is not false the first time it is being checked, then it will never be false the second or third time either, or any other time after that. Because the value will never spontaniously change.

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

    Heri623 (June 8th, 2014)

  4. #3
    Junior Member
    Join Date
    Jun 2014
    Location
    Northern Utah
    Posts
    13
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: infinite loop on pig latin program

    Quote Originally Posted by Cornix View Post
    Well, just think about it, your loop will stop when the following condition is false:
    wordToTranslate.equalsIgnoreCase("q")
    Now look at every single point in your program where the value of "wordToTranslate" is being set.
    You will realize that it is only set once, and that is at the very beginning of the program.
    So if the condition is not false the first time it is being checked, then it will never be false the second or third time either, or any other time after that. Because the value will never spontaniously change.
    I've tweaked this a little and it doesn't loop anymore but I lost the ability to quit when 'q' is entered.


     
    /******************
    *Pig Latin Program
     
    Eddy Caraballo
    ******************/
     
    import java.util.Scanner;
    public class PigLatin5 {
     
       public static void main(String[] args)
       {
          System.out.println("give me a word to translate or press 'q' to quit");    //displaying question//
          Scanner scan = new Scanner(System.in);       
     
          String wordToTranslate = scan.nextLine();                                  //taking user input and creates new variable//
          char v = Character.toLowerCase(wordToTranslate.charAt(0));                 //looking for the first letter of the word that is being input//
     
          while (wordToTranslate != "q")
          {
             System.out.println("give another word or press 'q' to quit");
             wordToTranslate = scan.nextLine();
     
     
          if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u')              //if first letter of inputted word is any of these letters(vowels)//
          {
              String pigLatinTranslation = wordToTranslate + "way";                  //then create new word with way at the end//
              System.out.println(pigLatinTranslation);                               //displays new word//
          }
     
          else                                                                       //otherwise//
          {
             String firstLetter = wordToTranslate.substring(0,1);                    //creates a new variable named firstLetter that comes from a section(substring) of "wordToTranslate" starting at character 0 and ending at character 1//
             String endOfWord = wordToTranslate.substring(1,wordToTranslate.length()); //creates a new variable named endOfWord that comes from a section(substring) of "wordToTranslate" starting at character 1 and ending at the length of the word//
             System.out.println (endOfWord + firstLetter + "ay");
     
          }
     
          }
     
       }  
     
    }

  5. #4
    Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    373
    Thanks
    0
    Thanked 64 Times in 54 Posts

    Default Re: infinite loop on pig latin program

    The condition in your first version was not wrong, it was actually correct. The changes you made to the condition, however, are not correct.
    You need to compare strings using the "equals" method. Comparing with "==" does not work for strings in java.

Similar Threads

  1. Pig Latin translator
    By geabus1043 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 4th, 2013, 09:43 AM
  2. Whats wrong with this Pig Latin code?
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 6th, 2011, 01:57 PM
  3. having problems with my pig latin translator (the translating portion)
    By WontonSoup in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 12th, 2011, 05:45 PM
  4. Help with Pig Latin Translator...
    By jchan in forum AWT / Java Swing
    Replies: 3
    Last Post: September 28th, 2009, 02:54 PM
  5. Pig Latin Translator
    By BMN901 in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: June 17th, 2009, 03:31 AM