The reason it gives you trouble is because when the user enters an integer then hits enter, two things have just been entered - the integer and a "newline" which is \n. The method you are calling, "nextInt", only reads in the integer, which leaves the newline in the input stream. But calling nextLine() does read in newlines, which is why you had to call nextLine() before your code would work. You could have also called next(), which would also have read in the newline.
BestJewSinceJC
Posting Maven
2,774 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
Skill Endorsements: 13
Question Answered as of 3 Years Ago by
cgeier,
BestJewSinceJC
and
jmaat7