1

I am trying to get input from user with a for loop but for example if user enters "2" as a "noOfSubPattern" loop does not wait for the second input. I cannot find what is wrong in my code.

    int noOfSubPattern;
    String subPattern = "";
    noOfSubPattern = scan.nextInt();        


    for(int i = 0; i < noOfSubPattern; i++)
    {

        subPattern += scan.nextLine();
    }

1 Answer 1

1

You have to discard the line terminator after nextInt()

noOfSubPattern = scan.nextInt();
scan.nextLine(); // Discard line terminator

for(int i = 0; i < noOfSubPattern; i++) // ...
Sign up to request clarification or add additional context in comments.

1 Comment

You are very welcome. Please accept the answer so that other people in the stackoverflow community can benefit.

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.