为什么扫描仪无法读取单词之间的任何(空)space(在我的特定情况下)?
Why scanner can't read any (empty) space between words (in my specific case)?
我的程序会要求用户输入 his/her 姓名。目前我的扫描仪对象只能读取一个单词。但是如果用户输入了多个单词(比如说某人的全名),那么它就会抛出异常。像这样:
我该如何解决这个问题?
我的代码:
public class BankApp_Assignment {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int userChoose;
String name = null;
int accNum = 0;
double InitiateAmount = 0;
double newAmm = 0;
double depOrWith = 0;
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "...................\n"
+ "...................\n\n"
+ "Choose your optiin:\n"
+ "1. Create new account\n"
+ "2. Deposit\n"
+ "3. View details\n"
+ "4. Quit\n\n");
while (true) {
userChoose = sc.nextInt();
if (userChoose == 1) {
System.out.println("Enter your full name:");
name = sc.next();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
System.out.println("Enter an initiating amount:");
InitiateAmount = sc.nextDouble();
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 2) {
System.out.println("Enter negative value to withdraw and positive to deposit");
depOrWith = sc.nextInt();
if (depOrWith < 0) {
newAmm = InitiateAmount + depOrWith;
} else {
newAmm = InitiateAmount + depOrWith;
}
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 3) {
System.out.println("Your name: " + name);
System.out.println("Your account number: " + accNum);
System.out.println("Your current balance: " + newAmm);
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 4) {
System.exit(0);
}
}
}
}
你需要改变
name = sc.next();
sc.next() 将只读到 space 之后什么都没有
name = sc.nextLine();
sc.nextLine() 将读取整行。
请注意,您需要小心跳过阅读 Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods。
我的程序会要求用户输入 his/her 姓名。目前我的扫描仪对象只能读取一个单词。但是如果用户输入了多个单词(比如说某人的全名),那么它就会抛出异常。像这样:
我该如何解决这个问题?
我的代码:
public class BankApp_Assignment {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int userChoose;
String name = null;
int accNum = 0;
double InitiateAmount = 0;
double newAmm = 0;
double depOrWith = 0;
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "...................\n"
+ "...................\n\n"
+ "Choose your optiin:\n"
+ "1. Create new account\n"
+ "2. Deposit\n"
+ "3. View details\n"
+ "4. Quit\n\n");
while (true) {
userChoose = sc.nextInt();
if (userChoose == 1) {
System.out.println("Enter your full name:");
name = sc.next();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
System.out.println("Enter an initiating amount:");
InitiateAmount = sc.nextDouble();
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 2) {
System.out.println("Enter negative value to withdraw and positive to deposit");
depOrWith = sc.nextInt();
if (depOrWith < 0) {
newAmm = InitiateAmount + depOrWith;
} else {
newAmm = InitiateAmount + depOrWith;
}
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 3) {
System.out.println("Your name: " + name);
System.out.println("Your account number: " + accNum);
System.out.println("Your current balance: " + newAmm);
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 4) {
System.exit(0);
}
}
}
}
你需要改变
name = sc.next();
sc.next() 将只读到 space 之后什么都没有
name = sc.nextLine();
sc.nextLine() 将读取整行。
请注意,您需要小心跳过阅读 Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods。