0

Here is a piece of the code

    System.out.println("How would you describe your lifestyle? Sedentary, Somewhat Active, Active, Highly Active?");
    String lifestyle = keyboard.next(); 
 if (lifestyle.equalsIgnoreCase("Somewhat Active"))
 {
  System.out.println("ok");
 }
 else
 {
  System.out.println("not ok")
 }

no matter what I type I cannot get an "ok" response.

0

2 Answers 2

2

Scanner#next() will always return the next token, which will just be "Somewhat". Use keyboard.nextLine() instead.

Sign up to request clarification or add additional context in comments.

2 Comments

but how "Somewhat".equalsIgnoreCase("Somewhat Active") will it return true?
@JunedAhsan No, the two strings aren't equal. "someWHAT eQuAL" would be acceptable in such a comparison, however. The OP is currently using next() which returns just "somewhat" which would of course not compare as intended.
0

You should try nextLine() as suggested by hexafraction.

String lifestyle = keyboard.next(); //<-- input without space, Somewhat
String lifestyle = keyboard.nextLine(); //<-- input with spaces, Somewhat Active

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.