469,271 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Help with program, while loops and equals()

KoreyAusTex
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:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. /**
  3.  * Program that outputs lyrics to Old Macdonald, by asking the user to enter a type of animal 
  4.  * and it's noise or no more to end the program
  5.  */
  6. public class OldMacDonald 
  7. {
  8.  
  9.     public static void main (String[] args) 
  10.     {
  11.         Scanner stdin = new Scanner(System.in);
  12.  
  13.         System.out.println("Let's sing \"Old MacDonald had a farm\": ");
  14.         System.out.println("Please enter an animal and a noise, or no more to stop playing.");
  15.         String animal = stdin.nextLine();
  16.  
  17.         while (!animal.equals("no more")) 
  18.         { 
  19.             break;
  20.         }
  21.  
  22.         String noise = stdin.next();
  23.         String stop = stdin.next();
  24.  
  25.  
  26.         printFirstLast();
  27.         printMiddleVerse(animal, noise);
  28.         printFirstLast();
  29.     }
  30.  
  31.     public static void printFirstLast() 
  32.     {
  33.         System.out.println("Old MacDonald had a farm, E-I-E-I-O.");    
  34.     }
  35.  
  36.     public static void printMiddleVerse(String animal, String noise)    
  37.     {
  38.         System.out.println("And on that farm he had some " + animal + ", E-I-E-I-O ");
  39.         System.out.println("With a " + noise + "-" + noise + " here, and a " + noise + "-" + noise + " there");
  40.         System.out.println("Here a " + noise + ", there a " + noise);
  41.         System.out.println("Everywhere a " + noise + "-" + noise);   
  42.     }
  43.  
  44. }
  45.  
Feb 16 '08 #1
16 4456
sicarie
4,677 Expert Mod 4TB
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...
Feb 16 '08 #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...
We are supposed to use equals() for comparing strings not the == != etc.
Feb 16 '08 #3
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
Feb 16 '08 #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!!
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.equals("no more")) break;//get out of while

//otherwise use STRING TOKENIZER
StringTokenizer data = StringTokeniezer(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.
Feb 16 '08 #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.equals("no more")) break;//get out of while

//otherwise use STRING TOKENIZER
StringTokenizer data = StringTokeniezer(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.
Feb 16 '08 #6
JosAH
11,448 Expert 8TB
Expand|Select|Wrap|Line Numbers
  1. while (!animal.equals("no more")) 
  2.         { 
  3.             break;
  4.         }
  5.  
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
Feb 16 '08 #7
sicarie
4,677 Expert Mod 4TB
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.
Feb 16 '08 #8
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.
Feb 16 '08 #9
I switched the code around a bit but it is still waiting for input even after I entered "no more"
Expand|Select|Wrap|Line Numbers
  1.  while (!"no more".equals(animal)) 
  2.         { 
  3.             System.out.println("Thanks for playing!");
  4.             break;
  5.         }
  6.  
Feb 17 '08 #10
JosAH
11,448 Expert 8TB
I switched the code around a bit but it is still waiting for input even after I entered "no more"
Expand|Select|Wrap|Line Numbers
  1.  while (!"no more".equals(animal)) 
  2.         { 
  3.             System.out.println("Thanks for playing!");
  4.             break;
  5.         }
  6.  
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
Feb 17 '08 #11
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!
Feb 17 '08 #12
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?
Expand|Select|Wrap|Line Numbers
  1. public class OldMcD 
  2. {
  3.  
  4.     public static void main(String[] args) 
  5.     {
  6.         Scanner stdin = new Scanner(System.in);
  7.  
  8.         System.out.println("Let's sing \"Old MacDonald had a farm\": ");
  9.         System.out.println("Please enter an animal and noise, or no more to stop playing.");
  10.         String animal = stdin.next();
  11.         String noise = stdin.next();
  12.  
  13.         while ((!"no".equals(animal)) & (!"more".equals(noise)))
  14.         {
  15.             System.out.println();
  16.             printFirstLast();
  17.             printMiddleVerse(animal, noise);
  18.             printFirstLast();
  19.             System.out.println();
  20.             System.out.println("Please enter an animal and noise, or no more to stop playing.");
  21.             animal = stdin.next();
  22.             noise = stdin.next();
  23.         }
  24.  
  25.     }
  26.  
  27.     public static void printFirstLast() 
  28.     {
  29.         System.out.println("Old MacDonald had a farm, E-I-E-I-O.");    
  30.     }
  31.  
  32.     public static void printMiddleVerse(String animal, String noise)    
  33.     {
  34.         System.out.println("And on that farm he had some " + animal + ", E-I-E-I-O ");
  35.         System.out.println("With a " + noise + "-" + noise + " here, and a " + noise + "-" + noise + " there");
  36.         System.out.println("Here a " + noise + ", there a " + noise);
  37.         System.out.println("Everywhere a " + noise + "-" + noise);   
  38.     }
  39.  
  40. }
  41.  
Feb 18 '08 #13
jmik16
2
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?
Feb 18 '08 #14
r035198x
13,262 8TB
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.
Feb 18 '08 #15
jmik16
2
yeah i used it for reference but i actually figured it out i was including the right variable name in the method
Feb 18 '08 #16
Ok, used the pipes instead of & in the while loop and I have this now! Thanks for the advice!
Feb 18 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

15 posts views Thread by chahnaz.ourzikene | last post: by
10 posts views Thread by Pavan | last post: by
6 posts views Thread by J | last post: by
7 posts views Thread by Buck Rogers | last post: by
3 posts views Thread by bumwipe | last post: by
KoreyAusTex
9 posts views Thread by KoreyAusTex | last post: by
2 posts views Thread by bmbvm5 | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.