sc.nextLine()无法读取行。

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

sc.nextLine() does not read line

问题

  1. 我有一个`Bank`一个`Consumer`以及一个名为`consumer.txt`的文件其中包含消费者的数据`Bank`类中有一个简单的方法用于提供消费者服务
  2. public static void bankService(String file) {
  3. String[][] customersString = readFileToString(file);
  4. customers = readFile(file);
  5. Scanner sc = new Scanner(System.in);
  6. double cash;
  7. String accNum;
  8. for (int i = 0; i < customers.length; i++) {
  9. customers[i].name = customersString[i][0];
  10. customers[i].surname = customersString[i][1];
  11. customers[i].balance = Integer.parseInt(customersString[i][2]);
  12. customers[i].accountNumber = customersString[i][3];
  13. System.out.println("Enter your account number: ");
  14. accNum = sc.nextLine();
  15. if (accNum.equals(customers[i].accountNumber)) {
  16. System.out.println("Your balance is: " + customers[i].balance);
  17. System.out.println("If you want to deposit cash press '1', if you want to withdraw cash press '2'");
  18. int choice = sc.nextInt();
  19. switch (choice) {
  20. case 1 -> {
  21. cash = sc.nextDouble();
  22. System.out.println(customers[i].name + " " + customers[i].surname +
  23. " " + customers[i].cashDeposit(cash));
  24. }
  25. case 2 -> {
  26. cash = sc.nextDouble();
  27. System.out.println(customers[i].name + " " + customers[i].surname +
  28. " " + customers[i].cashWithdraw(cash));
  29. }
  30. }
  31. }
  32. }
  33. }
  34. 当循环中的 i 大于 0 `accNum = sc.nextLine()` 不会获取我输入的数据`customers[i].accountNumber`会显示正确的数据`accNum` 只是空的但对于 `i = 0`它正常工作
  35. txt 文件格式为 "name" "surname" balance "account number"
  36. JAN KOWALSKI 20000 012345678912
  37. ANNA GRODZKA 35000 987654321098
  38. KUBA NOWAK 15000 789456123078
  39. 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.

  1. public static void bankService(String file) {
  2. String[][] customersString = readFileToString(file);
  3. customers = readFile(file);
  4. Scanner sc = new Scanner(System.in);
  5. double cash;
  6. String accNum;
  7. for (int i = 0; i &lt; customers.length; i++) {
  8. customers[i].name = customersString[i][0];
  9. customers[i].surname = customersString[i][1];
  10. customers[i].balance = Integer.parseInt(customersString[i][2]);
  11. customers[i].accountNumber = customersString[i][3];
  12. System.out.println(&quot;Enter your account number: &quot;);
  13. accNum = sc.nextLine();
  14. if (accNum.equals(customers[i].accountNumber)) {
  15. System.out.println(&quot;Your balance is: &quot; + customers[i].balance);
  16. 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;);
  17. int choice = sc.nextInt();
  18. switch (choice) {
  19. case 1 -&gt; {
  20. cash = sc.nextDouble();
  21. System.out.println(customers[i].name + &quot; &quot; + customers[i].surname +
  22. &quot; &quot; + customers[i].cashDeposit(cash));
  23. }
  24. case 2 -&gt; {
  25. cash = sc.nextDouble();
  26. System.out.println(customers[i].name + &quot; &quot; + customers[i].surname +
  27. &quot; &quot; + customers[i].cashWithdraw(cash));
  28. }
  29. }
  30. }
  31. }
  32. }

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"

  1. JAN KOWALSKI 20000 012345678912
  2. ANNA GRODZKA 35000 987654321098
  3. KUBA NOWAK 15000 789456123078
  4. 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:

确定