-4

So, what I am trying to do is get my while loops to do different things based on different key words. What I want is that when I type yes, I want it to exit the program, when I type no, for it to print a new song, and if there is a spelling error, for a message to pop up and allow the user to re enter yes or no to print the same song. I have 4 or 5 songs that I want to print out based on these three commands. In my last while loop. I want it to repeat the song every time the user types continue, and for it to end when the user types yes. I want the same thing to happen where it prompts the user to type again if there is a spelling error. This worked before, and now it does not. When I make a spelling error, it prompts me to enter again but will not print out the song in that loop. It will send me to the last while loop, ignoring all code in between. That last loop will only recognize spelling errors and yes, not continue, even though it did before.

Here is one song loop and my last loop:

import java.io.*;
import java.util.*;
public class FullNurseryRhymes
{
   public static void main(String[] args)
   {
       Scanner input = new Scanner(System.in);
       String aBCs, neverEnds, frogs, frogs2, monkeys, hdd, hdd2;

       NurseryRhymes rhymes = new NurseryRhymes(); 
    {
           System.out.println("Is the baby asleep? yes\\no");
           frogs=input.next();

           int byeFrog = 2;
           for(int i = 3; i >= 1; i--)
           {
              if (frogs.equalsIgnoreCase("no"))
              {
                 System.out.print(i + " " + rhymes.getFrogs());
                 System.out.println(" " + byeFrog + " " + rhymes.getFrogs2());
                 byeFrog -= 1;
              }
              else if (frogs.equalsIgnoreCase("yes"))
              {
                 System.out.println("The baby is asleep");
                 System.exit(0);
              }
              while(!frogs.equalsIgnoreCase("no"))
              {
                 System.out.println("Non requested input, please retry.");
                 System.out.println("\nIs the baby asleep? continue\\yes");
                 frogs = input.next();
                 if(frogs.equalsIgnoreCase("no"))
                  {
                      System.out.print(i + " " + rhymes.getFrogs());
                      System.out.println(" " + byeFrog + " " + rhymes.getFrogs2());
                      byeFrog -= 1;
                  } 
                  else if (frogs.equalsIgnoreCase("yes"))
                  {
                      System.out.println("The baby is asleep");
                      System.exit(0);
                  }
              }
           }
          }   


    //last loop 
         {
           System.out.println("Is the baby asleep? continue\\yes");
           neverEnds = input.next();
           while(neverEnds.equalsIgnoreCase("continue"))
           {
               System.out.println(rhymes.getNeverEnds());
               System.out.println("Is the baby asleep? continue\\yes");
               neverEnds = input.next();
           }
           if(neverEnds.equalsIgnoreCase("yes"))
           {
               System.out.println("The baby is asleep");
               System.exit(0);
           }
           while(!neverEnds.equalsIgnoreCase("continue")||!neverEnds.equalsIgnoreCase("yes"))
           {
                System.out.println("Non requested input, please retry");
                System.out.println("\nIs the baby asleep? continue\\yes");
                neverEnds = input.next();
                while (neverEnds.equalsIgnoreCase("continue"))
                   {
                       System.out.println(rhymes.getNeverEnds());
                       System.out.println("Is the baby asleep? continue\\yes");
                       neverEnds = input.next();
                       if(neverEnds.equalsIgnoreCase("yes"))
                       {
                           System.out.println("The baby is asleep");
                           System.exit(0);
                       }
                   }
                if (neverEnds.equalsIgnoreCase("yes"))
                   {
                       System.out.println("The baby is asleep");
                       System.exit(0);
                   }
           }
           }  
  • Please explain why this question is getting down votes. At least leave a comment why. I thought this site was supposed to help users with questions. If you leave no feed back, how am I supposed to know why you didn't like my question. – StaticStacksStack Oct 30 '16 at 00:10
  • 2
    *how am I supposed to know why you didn't like my question* By reading [ask], the first page you're presented with on this site, and making a [mcve]. – Tunaki Oct 30 '16 at 00:13
  • is that better? I thought my first question was clear. I did read that section when I joined – StaticStacksStack Oct 30 '16 at 00:19
  • 1
    Not really. There is still _a lot_ of code here, you need to cut this down to the relevant parts only. It shouldn't be more than 20 or so lines. Also, try to explain clearly and succinctly what you're trying to do, there's a big wall of text at the beginning here; it doesn't help at all. – Tunaki Oct 30 '16 at 00:21
  • alright, I fixed that. I made my paragraph shorter and only showed you the two troubling sections of my code. The first essentially repeats 5 times before the last – StaticStacksStack Oct 30 '16 at 00:44
  • `while(!neverEnds.equalsIgnoreCase("continue")||!neverEnds.equalsIgnoreCase("yes"))` -> [Why non-equality check of one variable against many values always returns true?](http://stackoverflow.com/questions/26337003/why-non-equality-check-of-one-variable-against-many-values-always-returns-true) – Pshemo Oct 30 '16 at 00:46
  • Having something that we can run from our IDEs that's small and still has the same issues would be more ideal. I will say that this has the smell of `Scanner.next` not being properly flushed... – Makoto Oct 30 '16 at 00:47
  • Im not sure. Im rather new to java and I knew I had to put a statement there. But ts my first time using a while with a not statement. – StaticStacksStack Oct 30 '16 at 00:48
  • @Makoto would you like me to include my scanner portion in there? – StaticStacksStack Oct 30 '16 at 00:50
  • At least declare the variables you're using. I can live with creating a Scanner instance. – Makoto Oct 30 '16 at 00:50
  • I would have to upload my other class, granted, which is small, so you can see the variables being inputted into the project – StaticStacksStack Oct 30 '16 at 00:52

2 Answers2

0

Based on the comments, it sounds like there is too much (potentially) relevant code for us to plough through.

There are three ways you could proceed with this.

  • You could learn how to use the debugger in your IDE. Use breakpoints and single stepping, and figure out where your mistakes are.

  • You could comment out parts of the code to track down where the problems are. That may also help you to create an MCVE

  • You could simplify the code by refactoring common / repetitious code into methods. For instance the "last loop" section is incredibly repetitious.

On the last point, it might actually be less work to throw this code away and start again ... after figuring out what common code can be implemented as methods.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
-3

Your question is a bit long. We don't need a story, just an explanation of your problem and a BIT of code, not your whole class. Try putting your while loop on the outside. Have a string outside the while loop called babySleep that starts as "no". Then, while(babySleep.equals("no") execute your code. Then at the end, check if the baby is a sleep, if he is, move on, if not, the while loop will re-execute. Also, instead of .equals try .equalsIgnoreCase so the user can type in "Yes" or "yES" etc.

Gabe Spound
  • 568
  • 5
  • 28