-1
//Question4 
correct = false;
System.out.println("how many feet are in a yard?");
while (!correct)
{
    x1 = input.nextInt();
    if (x1==3)
    {
        System.out.println("correct.");
        correct = true;
    }
    else
    {
        System.out.println("incorrect. try again.");
    }
}


    //Question5
correct = false;
System.out.println("What gets wetter and wetter the more it dries?");
while (correct == false)
{

   s1 = input.nextLine();
   if (s1.equalsIgnoreCase("a towel")) // cheating is bad!
    {
        System.out.println("correct");
        correct = true;
    }

    else
    {
        System.out.println("incorrect. try again.");
    }
}

output how many feet are in a yard? 3 correct. What gets wetter and wetter the more it dries? incorrect. try again. gets printed even before asking user input.

VisioN
  • 138,460
  • 30
  • 271
  • 271
Rudy Duran
  • 61
  • 1
  • 3

1 Answers1

1

Change your first question like this:

while (!correct)
{
    x1 = input.nextInt();
    input.nextLine(); //NEW CODE
    if (x1==3)
    {
        System.out.println("correct.");
        correct = true;
    }
    else
    {
        System.out.println("incorrect. try again.");
    }
}

You must always do a nextLine() after a nextInt(). The newline has not been swallowed in the nextInt(), therefore the following nextLine() is swallowing that and not what you want.

tckmn
  • 55,458
  • 23
  • 108
  • 154
  • ok, thanks. found addiotnal explanation here: http://stackoverflow.com/questions/7056749/scanner-issue-when-using-nextline-after-nextxxx – Rudy Duran Mar 06 '13 at 23:50