> Need help with the Java code?

Need help with the Java code?

Posted at: 2014-12-18 
You almost have the name part right. Just a small change:

keyboardInput.nextLine(); // discard end of last line

String name = keyboardInput.nextLine();

See the warning about the previous input being a number? The nextDouble() method only consumes the characters in the number itself, and nothing else. When it returns, the rest of the line that contained the number is still in the Scanner object, even if that was just the '\n' newline that ends the line.

So, the first nextLine() call above discards that rest-of-the-line and the second nextLine() call reads the whole next line as one string. Use nextLine() instead of next() because next() will stop at the space after the first name instead of reading the whole name.

I want this code to be able to ouput a first and last name.

but for some reason, it is only outputting a first name.

I know you have to use nextLine() but I'm not sure how to use it.

Here's what I have so far:

/*This section needs to prompt for and read the name of

* the users. This code has been provided.

* You may assume that the user will enter his/her name

* with firstname followed by lastname with a single space between. */

/* The prompt for this section is given for you */

/* Be careful - the input may be more than one word,

* but the last thing read from the input stream was a number. */

System.out.print("What is your name?");

String name = keyboardInput.next();

keyboardInput.nextLine();

/*Then write a nice message to the users telling him/her

* (by name to pay the bill)

* e.g. Bob Smith, please pay $45.23412

* or Joe Rosenthal, please pay $23.0

* (again don't worry about too many or too few decimal digits)*/

System.out.println(name +", please pay $" + totalBill);

thank you!

String name = keyboardInput.nextline ();

and you do not need the statement that follows.

next () only reads in one word.

Note: I recommend you use a shorter name for your Scanner object. Just "keyboard" would be sufficient of even kb or kbin.