1

Using Scanner class to write a console program that takes user input of name, address etc as a String using sc.next().

However when I enter more than one word separated by spaces (e.g "123 Main Street") it jumps ahead and enters the separate words as separate strings into the next variables one by one.

System.out.println("Please enter forename: ");
    String fName = sc.next();
    System.out.println("Please enter surname: ");
    String sName = sc.next();
    System.out.println("Please enter your street address:");
    String street = sc.next();
    System.out.println("Please enter your city: ");
    String city = sc.next();
    System.out.println("Please enter your postcode: ");
    String postcode = sc.next();

This outputs:

Please enter forename: 
John
Please enter surname: 
Smith
Please enter your street address:
123 Main Street
Please enter your city: 
Please enter your postcode: 

I'm sure this has something to do with how the Scanner uses delmiters, the spaces are used to break the string entered into separate string but I don't know how to fix it.... Any help greatly appreciated.

DMcDonald
  • 89
  • 13

0 Answers0