Welcome to Dream.In.Code
Become a Java Expert!

Join 150,356 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,937 people online right now. Registration is fast and FREE... Join Now!




Using While Loops and equals()

 
Reply to this topicStart new topic

Using While Loops and equals(), writing a program to print out lyrics to OldMacDonald with y the user

KoreyAusTex
16 Feb, 2008 - 03:43 PM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2007
Posts: 11


My Contributions
This is the code and again all i want to do is have the user type in the animal and the noise it makes or no more to break out and stop which would probably be done after entering in a few animals. But I need for it to be able to break out even on the first go round, I can only use while and if-else loops or maybe a switch but no string tokenizers or anything higher than basic level understanding.

CODE

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"))
        {
            System.out.println("Thanks for playing!");
            break;
        }
            
        String noise = stdin.next();
        String no_more = 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);  
    }
  
}


I think there is a flaw in the logic but not sure? When I run it it just sits there waiting even if I type in "no more", this is probably something minor but I have been staring at this screen all last night and today and I need a fresh set of eyes, not asking for anyone to do this for me but maybe hints as to structure and pointing me in the right direction.
User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Using While Loops And Equals()
16 Feb, 2008 - 05:36 PM
Post #2

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
your logic is backwards while animal != no more you do your song not quit
User is offlineProfile CardPM
+Quote Post

KoreyAusTex
RE: Using While Loops And Equals()
16 Feb, 2008 - 06:42 PM
Post #3

New D.I.C Head
*

Joined: 2 Jul, 2007
Posts: 11


My Contributions
QUOTE(dontKnowJava @ 16 Feb, 2008 - 06:36 PM) *

your logic is backwards while animal != no more you do your song not quit


could it be done this way?

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();
        String noise = stdin.next();
        
        
        while (!animal.equals("no more"))
        {
            printFirstLast();
            printMiddleVerse(animal, noise);
            printFirstLast();
            break;
        }
        
        System.out.println("Thanks for playing!");
        
    }

    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);  
    }
  
}

User is offlineProfile CardPM
+Quote Post

Nayana
RE: Using While Loops And Equals()
16 Feb, 2008 - 06:52 PM
Post #4

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
That's a lot closer.

But you also need to get your inputs while you are inside the loop.

Change this:
CODE

        String animal = stdin.nextLine();
        String noise = stdin.next();
        
        
        while (!animal.equals("no more"))
        {
            printFirstLast();
            printMiddleVerse(animal, noise);
            printFirstLast();
            break;
        }

to this:

CODE

        String animal = stdin.nextLine();
        String noise = stdin.nextLine();
        
        
        while (!animal.equals("no more"))
        {

            printFirstLast();
            printMiddleVerse(animal, noise);
            printFirstLast();

            animal = stdin.nextLine();
            noise = stdin.nextLine();
        }


And you may also want to tell the user that you're asking for an animal, and sound EVERY time.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Using While Loops And Equals()
16 Feb, 2008 - 06:55 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
A few issues:
CODE

// this
while (!animal.equals("no more")) {
    System.out.println("Thanks for playing!");
    break;
}

// is identical to this
if (!animal.equals("no more")) {
    System.out.println("Thanks for playing!");
}


This doesn't do anything other than print something if a condition is met. In particular, it doesn't make you leave the program.

Also, your String no_more = stdin.next(); is kind of meaningless.

Here's a little pseudo code to get you going:
CODE

// this is a forever loop, your code inside will decide when you can leave.
while true do
    prompt user
    get input
    if input is exit flag then break from loop
    prompt user again
    get more input
    show results
end while


Hope this helps.

User is online!Profile CardPM
+Quote Post

KoreyAusTex
RE: Using While Loops And Equals()
17 Feb, 2008 - 04:26 PM
Post #6

New D.I.C Head
*

Joined: 2 Jul, 2007
Posts: 11


My Contributions
QUOTE(baavgai @ 16 Feb, 2008 - 07:55 PM) *

A few issues:
CODE

// this
while (!animal.equals("no more")) {
    System.out.println("Thanks for playing!");
    break;
}

// is identical to this
if (!animal.equals("no more")) {
    System.out.println("Thanks for playing!");
}


This doesn't do anything other than print something if a condition is met. In particular, it doesn't make you leave the program.

Also, your String no_more = stdin.next(); is kind of meaningless.

Here's a little pseudo code to get you going:
CODE

// this is a forever loop, your code inside will decide when you can leave.
while true do
    prompt user
    get input
    if input is exit flag then break from loop
    prompt user again
    get more input
    show results
end while


Hope this helps.


This is what I came up with, might be ugly but does the job perfectly:

CODE

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);  
    }
  
}


User is offlineProfile CardPM
+Quote Post

dontKnowJava
RE: Using While Loops And Equals()
17 Feb, 2008 - 04:35 PM
Post #7

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
yeah it works ok except a user has to type in noise even if animal is nomore try this
CODE

while (!animal.equals("no more"))
        {
            printFirstLast();
            printMiddleVerse(animal, noise);
            printFirstLast();
            animal = stdin.nextLine();
            if(animal.equals("no more"))
            break;
            noise = stdin.nextLine();
        }

        System.out.println("Thanks for playing!");

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month