Re: Scanner skipping input
What do you mean when you say it's not letting you? Do you receive an Exception, or does something else happen? Have you read through the API for the Scanner class? Namely, do you understand the difference between nextInt() and nextLine()? Where is the Scanner after each method is called?
Re: Scanner skipping input
When I run the program, this is what I get:
Please make a selection [1 - Add Client / 2 - Create Invoice / 3 - Quit]:
1
Please enter the client's name: Please enter the client's numerical ID: 123
Please enter the client's address: Please enter the number of hours worked: 40
I have read the Scanner class info, but don't see what I'm doing wrong. I believe NextInt() returns the next integer entered in the scanner object and NextLine() returns the next entry in the scanner (including spaces) as a String. I'm not sure what you mean by where is the Scanner after each method is called. My scanner is called "keyboard" so when I write keyboard.nextInt() it calls the scanner object right?
Process completed.
Re: Scanner skipping input
Pay close attention to when the new line is passed by the Scanner. With nextInt(), you won't pass over the new line (which you add when you hit enter after typing an int). Then when you call nextLine(), the new line is still there, so it thinks that you want the empty String that was "entered" between the int and the new line.
Re: Scanner skipping input
Quote:
I'm not sure what you mean by where is the Scanner after each method is called.
The scanner works with a stream of characters (characters you can see, and characters like spaces and newlines). Every time you call nextWhatever() something is returned and the position of the scanner within the stream is updated. The documentation is very explicit (but maybe not clear...) about where in the stream each call to nextXXX() leaves the scanner.
And this makes a big difference. Your "address = keyboard.nextLine();" will do exactly what it is documented to do: including possibly returning an empty string straight away. Nothing in the documentation says it must wait if there is already a new line in the stream.