日历输入忽略下一个输入行

时间:2015-01-25 13:50:04

标签: java calendar

无法理解这一点。

以下代码编译并运行良好,除了一个问题 - 在出生日期的inputReg行之后(日,月,年份),代码的下一部分将被跳过一些原因。

在此代码的情况下,程序在创建Calendar之后立即忽略tel = inputReg.nextLine();

我已经愚弄了出生日期部分的位置,并发现下一个inputReg.nextLine()部分如果被放置在出生日期之后将始终被跳过。

运行代码的这一部分:

  Scanner inputReg = new Scanner(System.in);

  //initialization of attributes
  String IC = "";
  String name = "";
  String tel = "";

  int day = 0;
  int month = 0;
  int year = 0;
  //for date

  System.out.print("Enter IC number: ");
  IC = inputReg.nextLine();

  Calendar dob = new GregorianCalendar(year, month, day);

  System.out.print("Enter name: ");
  name = inputReg.nextLine();

  System.out.println("Enter Date of Birth (DD/MM/YYYY ): ");
  day = inputReg.nextInt();
  month = inputReg.nextInt();
  year = inputReg.nextInt();

  System.out.print("Enter telephone number: ");
  tel = inputReg.nextLine();

  Customer c = new Customer(IC, name, dob, tel);
  customerList.add(c);

  System.out.println("Customer " + c.getName() + " registered successfuly.");
  System.out.println(tel);

给我这个输出:

Enter IC number: XX11
Enter name: Bob
Enter Date of Birth (DD/MM/YYYY ): 
10
10
2000
Enter telephone number: Customer Bob registered successfuly.

//ends

我知道我可以简单地将出生日期区域放在最底层,但我不满意这样做。

我的问题是:什么导致inputReg.nextLine()部分总是被跳过?

顺便说一句,我只是在代码与Calendar交互时才遇到这个问题,所以也许有一些我没有得到的新东西。

3 个答案:

答案 0 :(得分:1)

问题是您的nextInt方法无法读取您为year = inputReg.nextInt()输入值的新行字符。你可以通过两种方式解决它。

解决方案1 ​​

为捕获的nextLine

添加'\n'方法
year = inputReg.nextInt();
inputReg.nextLine();

解决方案2

仅使用nextLine()

读取输入
year = Integer.parseInt(inputReg.nextLine());

答案 1 :(得分:0)

我通过这样做来修复它(如果那是你想要的?)

    package com.stackoverflow.solutionmaker;

    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.Scanner;

    public class StackOverflowSolutionsRunner {

        public static void main(String[] args) {

             Scanner inputReg = new Scanner(System.in);

              //initialization of attributes
              String IC = "";
              String name = "";
              String tel = "";

              int day = 0;
              int month = 0;
              int year = 0;
              //for date

              System.out.print("Enter IC number: ");
              IC = inputReg.nextLine();

              Calendar dob = new GregorianCalendar(year, month, day);

              System.out.print("Enter name: ");
              name = inputReg.nextLine();

              System.out.println("Enter Date of Birth (DD/MM/YYYY ): ");
              day = inputReg.nextInt();
              month = inputReg.nextInt();
              year = inputReg.nextInt();
              inputReg.nextLine(); // I added this line to fake an input

              System.out.print("Enter telephone number: ");
              tel = inputReg.nextLine();

              //Customer c = new Customer(IC, name, dob, tel);
              //customerList.add(c);

              //System.out.println("Customer " + c.getName() + " registered successfuly.");
              System.out.println(tel);      

} }
BTW - 注释掉了我无法从你那里看到的代码?

我相信来自nextLine()方法的这句话摘自Oracle文档是一个线索吗? :Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.

另外,nextInt()可能没有将输入重置为线路的生成? (只是假设)。

答案 2 :(得分:0)

在有用的暗示之后难以理解。在nextInt()之后和使用nextLine()之前没有'消耗'这一行,因此忽略了nextLine()。

相关问题