1

When I run the following code inside a do-while loop (omitted); it prints both lines asking for the name of a song & the size at the same time. However, it never collects a String input for the name before asking for the file size.

I need it to collect a String for the name before asking for the file size (which is not occurring).

How is this happening?

            //Interface.java
            import java.util.*;
            ...
            Scanner console = new Scanner(System.in); //After importing Scanner.
            ...

            option = console.nextInt(); // User chooses an option (add / delete song, etc.) by pressing a number. Only adding is relevant here.

            do { // Code inside a do-while loop which repeats for entering name / file size on 4 songs.
            song = console.nextInt(); // User chooses to add name / file size to either song1, song2, song3 or song 4 (there is a maximum of 4 song objects).

            System.out.println("Please enter the name of a song.");
            name = console.nextLine();

            System.out.println("Please enter file size in MB.");
            do { // This do-while checks input to be an integer / not negative & then it assigns the input as...
            fileSize = console.nextInt();
            // end of do-while loop which checks the integer.

            // end of do-while loop which lets user add name / size to a maximum of 4 songs.
  • 2
    Please try to provide a "minimum, but executable" code snippet. It is hard to say what is going on; without knowing how you setup your scanner ... – GhostCat Apr 19 '15 at 09:06
  • Your code seems to be working fine: http://ideone.com/RJhwiZ – Pshemo Apr 19 '15 at 09:11
  • 1
    http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods – maraca Apr 19 '15 at 09:11
  • @maraca There is no `nextLine` in OP example placed after `nextFoo`. – Pshemo Apr 19 '15 at 09:12
  • @Pshemo right, but there are other cases descriped, we don't know what he does *before* reading the name of the song. Are there any interactinos with the console before asking the name? – maraca Apr 19 '15 at 09:13
  • @maraca "*we don't know what he does before reading the name of the song*" yes, and that is why before guessing answers it is better to ask OP for [example with will let us reproduce problem](http://sscce.org/) like Jägermeister did. – Pshemo Apr 19 '15 at 09:18
  • @Pshermo I disagree, because I didn't post it as answer, I wasn't guessing it. It just could be the fast track to the solution if he finds the solution there. Why have him clean up and post everything then people go through his code, just to discover it could have been solved with this link? – maraca Apr 19 '15 at 09:21
  • 1
    It's confusing to call a `Scanner` `console` when there is also a [`Console`](https://docs.oracle.com/javase/7/docs/api/java/io/Console.html). – Boris the Spider Apr 19 '15 at 09:22
  • Possible duplicate: [Skipping nextLine() after use next(), nextInt() or other nextFoo() methods](http://stackoverflow.com/questions/13102045/skipping-nextline-after-use-next-nextint-or-other-nextfoo-methods) – Pshemo Apr 19 '15 at 09:26
  • I think I know the answer: print out content of name, is it: "Please enter the name of a song."? I guess you know where I'm going with this... – maraca Apr 19 '15 at 09:28
  • No, it's even funnier, my first link really solves the problem! `nextInt()` doesn't consume the newline character after `option = ...` execute an empty `console.nextLine();` and it should work. I thnk in the do while is also a problem if you enter wrong stufff first. – maraca Apr 19 '15 at 09:40
  • @maraca; It prints nothing. Also, it doesn't ever collect information for name as a String even though it asks for it. It basically prints both lines (enter name / file size) & immediately waits for an integer to be checked. – The Audience Apr 19 '15 at 09:41
  • It was solved once I put console.nextLine(); right after asking for a name. What exactly was happening? I've been programming for a few days. – The Audience Apr 19 '15 at 09:46

1 Answers1

2

Always execute console.nextLine(); after console.nextInt(); to consume tne newline character and it should work fine. See here: Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods The reason is that nextInt() doesn't consume the newline character ('\n'), so there is an empty line there waiting to be consumed.

Community
  • 1
  • 1
maraca
  • 7,323
  • 3
  • 20
  • 41