生日计算器格式异常错误

时间:2015-05-09 01:39:59

标签: java

我现在已经有了一些线程。特别是这一点在我的搜索中不断出现:

https://stackoverflow.com/questions/14219732/how-would-i-subtract-two-dates-mm-dd-yyyy

然而,它似乎没有做我想要的,或者它可能只是我不理解线程中的概念。基本上我只想接受用户输入(例如,12 17 1980)并从当前日期减去它以获得人的实际年龄。我从以前的线程和其他线程中读到的内容似乎只是为了进行这种简单的计算而变得如此复杂。截至目前,该程序已编译,但在输入日期后,我收到了这些错误:

Exception in thread "main" java.lang.NumberFormatException: For input string: "12 13 1981"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at bdayCalc.Class2.setAge(Class2.java:17)
at bdayCalc.Class1.main(Class1.java:20) 

到目前为止,我的程序中有以下内容:

package bdayCalc;

import java.util.Date;
import java.util.Scanner;

public class Class1 {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Class2 object2 = new Class2();

    System.out.print("What is your name? : ");
    String name = input.nextLine();
    object2.setName(name);
    object2.message();
    System.out.println();
    System.out.print("Please enter your birthday : ");
    String userInput = input.nextLine();
    object2.setAge(userInput);
    object2.finalMessage();
  }
}

package bdayCalc;
import java.util.Calendar;

public class Class2 {
int year = Calendar.DATE;
private String fullName;

public void setName(String name) {
    fullName = name;
}

private int bDay;
public void setAge(String userInput) {
    bDay = Integer.parseInt(userInput) - year;
}

/*private int year() {
    return 0;
}*/

public String getName() {
    return fullName;
}

public int getAge() {
    return bDay;
}

public void message() {
    System.out.printf("Hello %s ", getName());
}

public void finalMessage() {
    System.out.printf(getName() + " You were born in " + bDay);
}

}

2 个答案:

答案 0 :(得分:1)

我已经快速编写了一个适合您的简单示例。它使用Java 8 Date API:

 public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Please enter your birthday (YYYY-MM-DD) format: ");
        String userInput = input.nextLine();
        String[] splitValues = userInput.split("-");
        if(splitValues.length == 3) {
            int year = Integer.parseInt(splitValues[0]);
            int mon = Integer.parseInt(splitValues[1]);
            int day = Integer.parseInt(splitValues[2]);

            LocalDate birthdate = LocalDate.of(year, mon, day);
            LocalDate now =LocalDate.now();
            long age = Period.between(birthdate, now).getYears();
            System.out.println(age);
        }
    }

这是输出:

Please enter your birthday : 1981-10-22 33

答案 1 :(得分:0)

你可以试试这个:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Please enter your birthday as mm/dd/yyyy: ");
    while (true) {
        String userInput = input.nextLine();
        try {
            Date date = new SimpleDateFormat("mm/dd/yyyy").parse(userInput);
            long ageInDays = TimeUnit.DAYS.convert(new Date().getTime() - date.getTime(), TimeUnit.MILLISECONDS);
            int age = (int) ageInDays / 365;
            System.out.println(age);
            break;
        } catch (Exception ex) {
            System.out.println("Please enter a valide date");
        }
    }
}