英文:
sc.nextLine() does not read line
问题
我有一个`Bank`类,一个`Consumer`类,以及一个名为`consumer.txt`的文件,其中包含消费者的数据。在`Bank`类中有一个简单的方法用于提供消费者服务。
public static void bankService(String file) {
String[][] customersString = readFileToString(file);
customers = readFile(file);
Scanner sc = new Scanner(System.in);
double cash;
String accNum;
for (int i = 0; i < customers.length; i++) {
customers[i].name = customersString[i][0];
customers[i].surname = customersString[i][1];
customers[i].balance = Integer.parseInt(customersString[i][2]);
customers[i].accountNumber = customersString[i][3];
System.out.println("Enter your account number: ");
accNum = sc.nextLine();
if (accNum.equals(customers[i].accountNumber)) {
System.out.println("Your balance is: " + customers[i].balance);
System.out.println("If you want to deposit cash press '1', if you want to withdraw cash press '2'");
int choice = sc.nextInt();
switch (choice) {
case 1 -> {
cash = sc.nextDouble();
System.out.println(customers[i].name + " " + customers[i].surname +
" " + customers[i].cashDeposit(cash));
}
case 2 -> {
cash = sc.nextDouble();
System.out.println(customers[i].name + " " + customers[i].surname +
" " + customers[i].cashWithdraw(cash));
}
}
}
}
}
当循环中的 i 大于 0 时,`accNum = sc.nextLine()` 不会获取我输入的数据,而`customers[i].accountNumber`会显示正确的数据。`accNum` 只是空的,但对于 `i = 0`,它正常工作。
txt 文件格式为 "name" "surname" balance "account number"
JAN KOWALSKI 20000 012345678912
ANNA GRODZKA 35000 987654321098
KUBA NOWAK 15000 789456123078
TYMOTEUSZ KOLARZ 18000 654987321078
英文:
I have got Bank
class, Consumer
class and consumer.txt
with data of consumers. There is a simple method in Bank
class to do consumer service.
public static void bankService(String file) {
String[][] customersString = readFileToString(file);
customers = readFile(file);
Scanner sc = new Scanner(System.in);
double cash;
String accNum;
for (int i = 0; i < customers.length; i++) {
customers[i].name = customersString[i][0];
customers[i].surname = customersString[i][1];
customers[i].balance = Integer.parseInt(customersString[i][2]);
customers[i].accountNumber = customersString[i][3];
System.out.println("Enter your account number: ");
accNum = sc.nextLine();
if (accNum.equals(customers[i].accountNumber)) {
System.out.println("Your balance is: " + customers[i].balance);
System.out.println("If you want to deposit cash press '1', if you want to withdraw cash press '2'");
int choice = sc.nextInt();
switch (choice) {
case 1 -> {
cash = sc.nextDouble();
System.out.println(customers[i].name + " " + customers[i].surname +
" " + customers[i].cashDeposit(cash));
}
case 2 -> {
cash = sc.nextDouble();
System.out.println(customers[i].name + " " + customers[i].surname +
" " + customers[i].cashWithdraw(cash));
}
}
}
}
}
When the i in for loop is greater than 0, the accNum = sc.nextLine()
does not get data i enter while customers[i].accountNumber
shows correct data. The accNum is just empty, but for i = 0
it works fine.
txt file is "name" "surname" balance "account number"
JAN KOWALSKI 20000 012345678912
ANNA GRODZKA 35000 987654321098
KUBA NOWAK 15000 789456123078
TYMOTEUSZ KOLARZ 18000 654987321078
答案1
得分: 1
不要使用 accNum = sc.nextLine(); 我建议你使用 accNum = sc.next();,因为你的 accNum 不包含任何空格。
因为你使用了 int choice = sc.nextInt();,nextInt() 不会读取换行符,但是你会通过按 Enter 键创建新的一行。因此 accNum = sc.nextLine(); 会读取那个新的换行符并返回。
英文:
Instead of using accNum = sc.nextLine(); I suggest you to use accNum = sc.next(); since your accNum does not contain any spaces.
since you use int choice = sc.nextInt(); , nextInt() is not read new line character but you create new line by hitting Enter. Therefore accNum = sc.nextLine(); reads that new line character and returns.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论