0

I am having some issues with my scanner in my program.

System.out.print("Do you smoke?(y/n): ");       
String isSmoker = console.nextLine();
boolean smoker = false;
if (isSmoker.equalsIgnoreCase("y")) {
    smoker = true;
}

This part of the program is not letting me enter a response to the question, and instead just skips ahead to the next part of the program and then ends. Later on in the program, there are methods for calculating BMI and getting costs for insurance. When I run the program, this is what happens, for instance:

Welcome to our new Health Plan! 
When prompted, enter the health plan in which you would like to enroll:
E (E-mployee), C (Employee/C-hildren), S (Employee/S-pouse), F (Employee/F-amily).
You will be asked whether or not you smoke and for your weight and height.
If you choose plan S or F, you will be asked whether your spouse smokes and for
his/her weight and height. Your monthly health plan premium will then be displayed.
Heath Plan (E-mployee, C-hildren, S-pouse, F-amily): 
C
Weight (lb): 128
Height (in): 62
Do you smoke?(y/n): 23.408949011446413Monthly Health Plan Premium: 245
MacBook-Pro:src cs$ 

How can I make the question for smoking accept responses in the console? Am I to use nextBoolean instead of nextLine?

Shilpa Kancharla
  • 107
  • 1
  • 2
  • 7
  • 1
    You mean like http://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo ? – Scary Wombat Jun 26 '18 at 05:37
  • I don't see anything wrong with you small code snippet. Can you give us a reproducible example? – Tim Biegeleisen Jun 26 '18 at 05:38
  • Don't mix `nextLine()` with _other_ methods like `next()`, `nextInt()`, `nextDouble()` and so on; until you've read the question that Scary linked to. – Dawood ibn Kareem Jun 26 '18 at 05:39
  • Yes, now to my code I've added: `console.nextLine();` after `String isSmoker = console.nextLine();` and I am able to enter an input for the question. – Shilpa Kancharla Jun 26 '18 at 05:45

0 Answers0