0

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);
7
  • 3
    Have you tried stepping through the code in your debugger? What exactly is your problem? BTW you don't need to call System.out.flush(); Commented Jul 31, 2014 at 21:16
  • 3
    Looks to me like the issue is the close bracket after question = input.nextInt (); } Commented Jul 31, 2014 at 21:18
  • Well I don't want it to skip anything and I want the loop to exit properly. I haven't debugged it yet but even if i did, I wouldn't know exactly what to do. I am still new to coding. Commented Jul 31, 2014 at 21:19
  • /* Declarations */ int participants = 0; String [] ageGroup; ageGroup = new String [5]; ageGroup [0] = "Under 3"; ageGroup [1] = "3 through 7"; ageGroup [2] = "8 through 10"; ageGroup [3] = "11 through 13"; ageGroup [4] = "14 and older"; String authorName = " "; int num; int writtenBooks; int num1; String authorDemographics = null; String authorDemographics01 = null; int question = 0; int demoLoop; Commented Jul 31, 2014 at 21:20
  • My spider sense is saying that nextInt doesn't read in the new line, meaning that the new line is then fed into nextLine, and this is repeated over and over. Commented Jul 31, 2014 at 21:20

2 Answers 2

2

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.

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

Comments

0

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?

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.