Scanner input problem
This is a simple program that I have written in Grade 12 Information Technology, and we were instructed to do the following, which is to ask the user to enter an integer, then call a method and asked a user to enter their full name. Input is done using the methods from the Scanner class of the java.util.* package. When I run the program, the first part runs correctly and I enter a number but after that it calls the name() method and prints "Enter FULL name: " but then the program ends without giving me the chance to enter a name. If I use kbRead.next() instead it works with only one name but I need to use nextLine() to enter a name and surname. What am I doing wrong? Help will be appreciated.
public class test
static Scanner kbRead=new Scanner(System.in);
static String fname;
public static void main (Stringargs)
System.out.println("Enter a number: ");
public static void name()
System.out.print("Enter FULL name:");
Thanks, it works now. Could you please explain why it was doing that originally?
Originally Posted by hardwired
Because after the nextInt() method pulls the value from the keyboard buffer, it leaves the newline character that was entered. So when you use the nextLine() method next, it reads in the newline character and stops like its suppose to.
Unlike other Scanner methods, nextLine() doesn't ignore the first newline character. You could just write a statement like this:
to consume the newline character and then proceed to read in the user's string