Java Scanner类分隔符用法 - 如何调整

时间:2016-02-02 16:33:26

标签: java string delimiter

使用Scanner类编写一个控制台程序,该程序使用sc.next()将用户输入的名称,地址等作为字符串。

但是当我输入多个以空格分隔的单词(例如“123 Main Street”)时,它会向前跳转并将单独的单词作为单独的字符串逐个输入到下一个变量中。

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();

输出:

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

我确定这与扫描仪如何使用delmiters有关,这些空格用于打破输入到单独字符串中的字符串,但我不知道如何修复它....任何帮助都非常感激。

0 个答案:

没有答案