Help with program, while loops and equals()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KoreyAusTex
    New Member
    • Feb 2007
    • 36

    Help with program, while loops and equals()

    I am tired and lost, sitting here on a Friday night trying to finish my program. The program prompts the user to enter a series of animal names and sounds, and print the corresponding verses from the song "Old MacDonald had a farm". The program will continue printing verses until the user enters "no more" instead of an animal name and sound. This is what I have so far:

    Code:
    import java.util.*;
    /**
     * Program that outputs lyrics to Old Macdonald, by asking the user to enter a type of animal 
     * and it's noise or no more to end the program
     */
    public class OldMacDonald 
    {
      
        public static void main (String[] args) 
        {
            Scanner stdin = new Scanner(System.in);
            
            System.out.println("Let's sing \"Old MacDonald had a farm\": ");
            System.out.println("Please enter an animal and a noise, or no more to stop playing.");
            String animal = stdin.nextLine();
            
            while (!animal.equals("no more")) 
            { 
                break;
            }
                
            String noise = stdin.next();
            String stop = stdin.next();
            
            
            printFirstLast();
            printMiddleVerse(animal, noise);
            printFirstLast();
        }
     
        public static void printFirstLast() 
        {
            System.out.println("Old MacDonald had a farm, E-I-E-I-O.");    
        }
    
        public static void printMiddleVerse(String animal, String noise)    
        {
            System.out.println("And on that farm he had some " + animal + ", E-I-E-I-O ");
            System.out.println("With a " + noise + "-" + noise + " here, and a " + noise + "-" + noise + " there");
            System.out.println("Here a " + noise + ", there a " + noise);
            System.out.println("Everywhere a " + noise + "-" + noise);   
        }
      
    }
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Well, aside of recommending that you change your logic a bit to

    while (sound read in != "no more")
    {
    do all your parsing and printing
    }

    aside of that, I missed your question. Were you having trouble with part of it? Looks like you have a good start so far...

    Comment

    • KoreyAusTex
      New Member
      • Feb 2007
      • 36

      #3
      Originally posted by sicarie
      Well, aside of recommending that you change your logic a bit to

      while (sound read in != "no more")
      {
      do all your parsing and printing
      }

      aside of that, I missed your question. Were you having trouble with part of it? Looks like you have a good start so far...
      We are supposed to use equals() for comparing strings not the == != etc.

      Comment

      • kedmotsoko
        New Member
        • Feb 2008
        • 8

        #4
        you should no what you are taking into the progam...
        suppose you read-in "animal" and "noise";

        ask user to enter them in one line separated by space eg: cow moooo
        so it possible to enter: no more--like this!!
        //read like this
        input = studin.nextline ()
        //check

        Comment

        • kedmotsoko
          New Member
          • Feb 2008
          • 8

          #5
          you should no what you are taking into the progam...
          suppose you read-in "animal" and "noise";

          ask user to enter them in one line separated by space eg: cow moooo
          so it possible to enter: no more--like this!!
          while(true)
          {
          //prompt user user to enter animail and noise in one line separated by space or type "no more" to exit
          //read like this
          input = studin.nextline ()

          //check
          if(!input.equal s("no more")) break;//get out of while

          //otherwise use STRING TOKENIZER
          StringTokenizer data = StringTokenieze r(input);//check spelling for StringTok...
          animal = data.nextToken( );
          noise = data.nextToken( );

          ///do the rest of print>>>call you function
          }


          Hope i'm not making a lot of work for you....
          Enjoy.

          Comment

          • KoreyAusTex
            New Member
            • Feb 2007
            • 36

            #6
            Originally posted by kedmotsoko
            you should no what you are taking into the progam...
            suppose you read-in "animal" and "noise";

            ask user to enter them in one line separated by space eg: cow moooo
            so it possible to enter: no more--like this!!
            while(true)
            {
            //prompt user user to enter animail and noise in one line separated by space or type "no more" to exit
            //read like this
            input = studin.nextline ()

            //check
            if(!input.equal s("no more")) break;//get out of while

            //otherwise use STRING TOKENIZER
            StringTokenizer data = StringTokenieze r(input);//check spelling for StringTok...
            animal = data.nextToken( );
            noise = data.nextToken( );

            ///do the rest of print>>>call you function
            }


            Hope i'm not making a lot of work for you....
            Enjoy.
            We are not able to use String Tokenizer, we are supposed to use while loops and equals() and that it pretty much it.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              [code=java]
              while (!animal.equals ("no more"))
              {
              break;
              }
              [/code]

              This little loop doesn't make sense; think about it: suppose you typed 'cow'
              for the animal so it doesn't equal "no more" and the body of the loop is run:
              it breaks out of the loop immediately.

              Now suppose you typed 'no more' so the body of the loop isn't executed.

              kind regards,

              Jos

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by KoreyAusTex
                We are supposed to use equals() for comparing strings not the == != etc.
                Yes, that is pseudocode. As you had it correctly in your first snippet, I figured you would be able to make that leap, considering I had a variable (if taken literally) called 'sound read in' with spaces, I was trying to convey the logic, not the literal code.

                Comment

                • KoreyAusTex
                  New Member
                  • Feb 2007
                  • 36

                  #9
                  Originally posted by sicarie
                  Well, aside of recommending that you change your logic a bit to

                  while (sound read in != "no more")
                  {
                  do all your parsing and printing
                  }

                  aside of that, I missed your question. Were you having trouble with part of it? Looks like you have a good start so far...
                  I think I have been concentrating to hard on this and have not been letting it come to me, I am stuck and trying to implement all the suggestions I have found but no been able to understand.

                  Comment

                  • KoreyAusTex
                    New Member
                    • Feb 2007
                    • 36

                    #10
                    I switched the code around a bit but it is still waiting for input even after I entered "no more"
                    Code:
                     while (!"no more".equals(animal)) 
                            { 
                                System.out.println("Thanks for playing!");
                                break;
                            }

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by KoreyAusTex
                      I switched the code around a bit but it is still waiting for input even after I entered "no more"
                      Code:
                       while (!"no more".equals(animal)) 
                              { 
                                  System.out.println("Thanks for playing!");
                                  break;
                              }
                      This doesn't make any difference from what I explained in my previous reply.
                      Basically it reads: while you haven't typed "no more" print a goodbye message
                      once because you'll break out of the loop immediately.

                      That while loop should be the outermost structure in your program because you
                      *do* want to play if the user hasn't typed "no more".

                      kind regards,

                      Jos

                      Comment

                      • KoreyAusTex
                        New Member
                        • Feb 2007
                        • 36

                        #12
                        Originally posted by JosAH
                        This doesn't make any difference from what I explained in my previous reply.
                        Basically it reads: while you haven't typed "no more" print a goodbye message
                        once because you'll break out of the loop immediately.

                        That while loop should be the outermost structure in your program because you
                        *do* want to play if the user hasn't typed "no more".

                        kind regards,

                        Jos
                        Thank you for your help, I understand what you are say and will apply it to the program!

                        Comment

                        • KoreyAusTex
                          New Member
                          • Feb 2007
                          • 36

                          #13
                          Originally posted by JosAH
                          This doesn't make any difference from what I explained in my previous reply.
                          Basically it reads: while you haven't typed "no more" print a goodbye message
                          once because you'll break out of the loop immediately.

                          That while loop should be the outermost structure in your program because you
                          *do* want to play if the user hasn't typed "no more".

                          kind regards,

                          Jos
                          I think I have it now, can you tell me what you think, it does what I need it to do but I was wondering if you could comment on the efficiency of the code?
                          Code:
                          public class OldMcD 
                          {
                            
                              public static void main(String[] args) 
                              {
                                  Scanner stdin = new Scanner(System.in);
                                  
                                  System.out.println("Let's sing \"Old MacDonald had a farm\": ");
                                  System.out.println("Please enter an animal and noise, or no more to stop playing.");
                                  String animal = stdin.next();
                                  String noise = stdin.next();
                                          
                                  while ((!"no".equals(animal)) & (!"more".equals(noise)))
                                  {
                                      System.out.println();
                                      printFirstLast();
                                      printMiddleVerse(animal, noise);
                                      printFirstLast();
                                      System.out.println();
                                      System.out.println("Please enter an animal and noise, or no more to stop playing.");
                                      animal = stdin.next();
                                      noise = stdin.next();
                                  }
                                  
                              }
                           
                              public static void printFirstLast() 
                              {
                                  System.out.println("Old MacDonald had a farm, E-I-E-I-O.");    
                              }
                          
                              public static void printMiddleVerse(String animal, String noise)    
                              {
                                  System.out.println("And on that farm he had some " + animal + ", E-I-E-I-O ");
                                  System.out.println("With a " + noise + "-" + noise + " here, and a " + noise + "-" + noise + " there");
                                  System.out.println("Here a " + noise + ", there a " + noise);
                                  System.out.println("Everywhere a " + noise + "-" + noise);   
                              }
                            
                          }

                          Comment

                          • jmik16
                            New Member
                            • Feb 2008
                            • 2

                            #14
                            Hi, I have the same assignment and i am trying to go by with what you have started but when i compile the program it tells me that it cannot find the variable for animal? did this happen to you?

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by jmik16
                              Hi, I have the same assignment and i am trying to go by with what you have started but when i compile the program it tells me that it cannot find the variable for animal? did this happen to you?
                              Are you using the code in this thread? Maybe you should be using it only for reference rather than copying the whole thing.

                              Comment

                              Working...