473,581 Members | 2,755 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with program, while loops and equals()

KoreyAusTex
36 New Member
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 4793
sicarie
4,677 Recognized Expert Moderator Specialist
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
KoreyAusTex
36 New Member
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
kedmotsoko
8 New Member
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
kedmotsoko
8 New Member
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.
Feb 16 '08 #5
KoreyAusTex
36 New Member
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.
Feb 16 '08 #6
JosAH
11,448 Recognized Expert MVP
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 Recognized Expert Moderator Specialist
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
KoreyAusTex
36 New Member
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
KoreyAusTex
36 New Member
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

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

Similar topics

15
1626
by: chahnaz.ourzikene | last post by:
Hi all, This is the first i post in this newsgroup, i hope my english is not too bad... Let's get straight to the point ! I have a little probleme using threads in my little training example : I wish to create two threads in my application, one thread (the subject) will increment a variable, and another thread (the controller) will print a...
10
1873
by: Pavan | last post by:
Hi i have two nested loops as shown below: 1. for(i=0;i<=1000;i++) { for(i=0;i<=100;i++) { .....; .....; }
11
5823
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am using the split function with ' ', but how do i get it to take into account punctuation marks such as ',',',' etc? Also I am then trying to add...
6
3132
by: J | last post by:
Kind of new at programming/vb.net. I'm doing this junky die roller program. Heres's what is supposed to happen: Roll 2 6-sided dies. Add rolls together put total in rolls(d6total). Display the number of 2 or 12 rolled, then display those numbers and average rolls. I didn't know how to do it exactly, but I came up with this.
7
2165
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the "continue_function", and if NO is returned to "get_data", display_report executes and the program ends? Basically I am having trouble understanding the program...
3
7756
by: bumwipe | last post by:
I am trying to create a program that takes string expressions and solves them mathematically (i.e. string "5 + 4 * 2" will evaluate as 13). i have this much of the program made, but I am not sure why it is not working. it keeps telling me there is a "Number Format Exception" Please help! import java.util.ArrayList; import...
9
3065
KoreyAusTex
by: KoreyAusTex | last post by:
I have written a program using method calls as opposed to nested while loops, or rather a while within a while, but for some reason the only way I can get the program to terminate is to type break in the main method after the method calls. This is not the way I was supposed to do it, any thoughts? import java.util.*; public class...
2
2335
by: bmbvm5 | last post by:
Hello; I am beginner in java and I should submit one java program in early day. I try to write this program more then ten time but every time I find one broblem. Please help me because if you don't do that I am sure that I can not finish that before submited date. I write my assignment here also I write my codes please help me and guide me...
3
2957
by: smileyme74 | last post by:
Every time I enter for example 1 mile, I get the error. /** * This program will convert measurements expressed in inches, * feet, yards, or miles into each of the possible units of * measure. The measurement is input by the user, followed by * the unit of measure. For example: "17 feet", "1 inch", * "2.73 mi". Abbreviations in,...
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3832
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1409
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.