sc.nextLine()无法读取行。

huangapple go评论66阅读模式
英文:

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 &lt; 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(&quot;Enter your account number: &quot;);
accNum = sc.nextLine();
if (accNum.equals(customers[i].accountNumber)) {
System.out.println(&quot;Your balance is: &quot; + customers[i].balance);
System.out.println(&quot;If you want to deposit cash press &#39;1&#39;, if you want to withdraw cash press &#39;2&#39;&quot;);
int choice = sc.nextInt();
switch (choice) {
case 1 -&gt; {
cash = sc.nextDouble();
System.out.println(customers[i].name + &quot; &quot; + customers[i].surname +
&quot; &quot; + customers[i].cashDeposit(cash));
}
case 2 -&gt; {
cash = sc.nextDouble();
System.out.println(customers[i].name + &quot; &quot; + customers[i].surname +
&quot; &quot; + 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.

huangapple
  • 本文由 发表于 2020年10月17日 19:14:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/64401822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定