Reputation: 1
After giving the input to writtenBooks, it skips the option to enter input for authorDemographics and continues to the next prompt. At this point, regardless of the input i typed it continuously prompts me "Type 1 for Yes. Type 2 for No." Looking forward to any feedback. Thanks
/* body */
System.out.println ("Hello, welcome to the convention!");
System.out.println ("Please enter your full name: ");
authorName = input.nextLine ();
System.out.flush();
System.out.println ("Please enter the amount of books you have written: ");
writtenBooks = input.nextInt ();
System.out.println ("Who is your target demographic for your books?");
System.out.println ("Type: Under 3 , 3 through 7 , 8 through 10 , 11 through 13 , or 14 and older.");
authorDemographics = input.nextLine ();
System.out.flush();
/* loop */
demoLoop = 0;
while (!ageGroup [4].equals(authorDemographics) && demoLoop == 0) {
System.out.println ("Would you like to enter more demographics?");
System.out.println ("Type 1 for Yes. Type 2 for No.");
question = input.nextInt (); }
if (question == 1) {
System.out.println ("Who is your target demographic for your books?");
System.out.println ("Type: Under 3, 3 through 7, 8 through 10, 11 through 13, or 14 and older.");
authorDemographics01 = input.nextLine ();
System.out.flush();
} else {
demoLoop = 1;
System.out.println ("Author Name:" + authorName);
System.out.println ("First Demographic: " + authorDemographics);
System.out.println ("Second Demographic: " + authorDemographics01);
System.out.println ("Amount of books written: " + writtenBooks);
Upvotes: 0
Views: 110
Reputation: 15755
I think you are getting your indents and brackets mixed up.
For example lets look at your while loop ONLY
while (!ageGroup [4].equals(authorDemographics) && demoLoop == 0) {
System.out.println ("Would you like to enter more demographics?");
System.out.println ("Type 1 for Yes. Type 2 for No.");
question = input.nextInt (); }
You never change ageGroup[4]
or demoloop
, so once your loop hits the end, it checks the condition again, and the condition will always be true. (A loop in motion remains in motion unless acted upon)
It also looks like your if is indented. This does not mean that it is in your loop. Your loop end where the matching bracket is regardless of indentation. Did you want the if and else INSIDE of the loop?
Upvotes: 0
Reputation: 25950
Look at the brackets of your while loop. You never change the value of any of the operands of the exit condition, so it will naturally loop infinitely if it is true the first time.
Upvotes: 2