0

I have this code snippet:

    for(int i=0; i<3; i++){
        pers[i] = new personalInfo();
        System.out.printf("Please input the name for person %s: ", i );
        inName = input.nextLine();
        pers[i].setName(inName);
        System.out.println(pers[i].getName);
        System.out.printf("Please input the address for person %s: ", i );
        inAddress = input.nextLine();
        pers[i].setAddress(inAddress);
        System.out.println(pers[i].getAddress);
        System.out.printf("Please input the age for person %d: ", i );
        inAge = input.nextInt();
        pers[i].setAge(inAge);
        // input.nextLine();
        System.out.println(pers[i].getAge);
        System.out.printf("Please input the phone number for person %d, without dashes included (ex. 1112223333): ", i );
        inPhoneNumber = input.nextLong();
        pers[i].setPhoneNumber(inPhoneNumber);
        System.out.println(pers[i].getPhoneNumber);
        // input.nextLine();
    }

I get problems because nextInt() and nextLong() are not consuming the newlines. However, this can be solved by uncommenting only the LAST input.nextLine() (even though the loop called nextInt() and nextLong()). I don't understand why I don't need both input.nextLine() calls. It seems like there should be 2 newlines in the buffer at the end of the first iteration, but there is only one. Is this because nextInt() and nextLong() consume leading newlines up to the respective integer or long? Let me know if I'm not making sense.

Rick
  • 41
  • 6
  • 1
    I suspect that [this question](http://stackoverflow.com/questions/13102045/skipping-nextline-after-using-next-nextint-or-other-nextfoo-methods) might help - holding off marking as a dupe as I am not 100% certain. – Andy Turner Apr 18 '16 at 08:01
  • that completely depends on the input if input on the same line you need one nextLine() and if input are on different line you need nextLine() after each next(). Did you read [my comment](http://stackoverflow.com/questions/36687200/why-is-my-code-not-prompting-twice-for-input-only-on-the-second-iteration-of-th#comment60963870_36687200) – singhakash Apr 18 '16 at 08:03

0 Answers0