-1

The following while loop within the program I'm writing is not being entered into, even though the condition within parentheses would evaluate as true (as far as I can tell). Here is the loop with the assignment before it:

String choose = "y";
  while(choose=="y");  {
     System.out.println("Enter 4-digit time: (0000 - 2359)");
     String time_ = s.nextLine();
     System.out.println("Angle between hour hand and minute hand is: " + timeToAngle(time_) + " degrees");
     System.out.println("Would you like to calculate the angle for another time? (y/n)");
     choose = s.next();
  }

Please could somebody give me a pointer in the right direction? This is driving me crazy

Joe Preyer
  • 57
  • 5
  • Although he does have the comparing string issue, that's not what's causing the problem. Why is it a duplicate? – resueman Aug 15 '14 at 13:50
  • @resueman I thought that was the only problem when I closed it :-(. Although if I could vote twice, I'd now opt for the "cause by a typo" reason. – Duncan Jones Aug 15 '14 at 13:50
  • Same here, easy to overlook the semi-colon when there's a big glaring error right in front of your face. – Anthony Grist Aug 15 '14 at 13:51
  • It's a different question with the same solution - not sure if that qualifies as a duplicate. – Joe Preyer Aug 15 '14 at 13:54
  • @JoePreyer It's a grey area, deciding what's a duplicate. In theory, the question itself must be a duplicate, not the answer. In your case, having missed the sneaky `;` error, I opted for closure to avoid 15 people posting answers pointing out the `==` error. It was a bad call this time :-) – Duncan Jones Aug 15 '14 at 13:55
  • Ah alright, I see what you mean. No worries, thanks for all the help everyone :) – Joe Preyer Aug 15 '14 at 13:57

2 Answers2

3

try this:

while(choose=="y"); { -> while(choose.equals("y")) {

  • use equals() to compare strings
  • remove the ;
Kent
  • 189,393
  • 32
  • 233
  • 301
2

There is a ; just after the while; it is making it constantly stay inside the while loop (if the condition is met) never entering the code inside brackets.

Besides, it seems that you're using the wrong way to analyse the value of that variable (as others have pointed out). Have a look at this SO question for the proper way how to do it.

Community
  • 1
  • 1
Momergil
  • 2,213
  • 5
  • 29
  • 59
  • Ah thank you! Man that was a dumb mistake. I've changed it now and it enters the loops, but even if I enter y when it prompts me at the end of the loop, it exits the loops... any idea why? – Joe Preyer Aug 15 '14 at 13:51