为什么我的程序返回null,null,0,0?

时间:2015-11-19 23:16:31

标签: java

我的程序应该将用户提交的信息存储到另一个类中。然后应该执行一个小的计算来确定年龄。最后一部分是在Driver中创建辅助类的实例,然后使用toString打印出所有信息。我的结果每次都是null,null,0,0。我做错了什么?

public class CustomerInfoProgram {

    public static Scanner scan = new Scanner(System.in);
    public static String firstName;
    public static String lastName;
    public static String address;
    public static String phoneNumber;
    public static int dob;
    public static int currentYear;
    public static int age;
    public static String name;

    public static void main(String[] args) {

        Customer in = new Customer();

        getCustomerName();
        getPhone();
        getAddress();
        getDOB();
        getCurrentYear();
        in.toString();

    }

    public static String getCustomerName() {

        System.out.println("What is your first name?");
        firstName = scan.nextLine();

        System.out.println("What is your last name?");
        lastName = scan.nextLine();

        name = firstName + " " + lastName;

        return name;

    }

    public static String getAddress() {

        System.out.println("What is your address?");
        address = scan.nextLine();

        return address;
    }

    public static String getPhone() {

        System.out.println("What is your phone number?");
        phoneNumber = scan.nextLine();

        return phoneNumber;
    }

    public static int getDOB() {

        System.out.println("What year were you born?");
        dob = scan.nextInt();

        return dob;
    }

    public static int getCurrentYear() {

        System.out.println("What is the current year?");
        currentYear = scan.nextInt();

        return currentYear;
    }
}

中学课程:

import java.util.Scanner;

public class Customer {

    public static int age;
    public static String allInfo;
    public static String name = CustomerInfoProgram.name;
    public static String address = CustomerInfoProgram.address;
    public static String phoneNumber = CustomerInfoProgram.phoneNumber;
    public static int dob = CustomerInfoProgram.dob;
    public static int currentYear = CustomerInfoProgram.currentYear;

    private int getAge() {

        age = (currentYear - dob);

        return age;
    }

    public String toString() {

        getAge();
        allInfo = (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
        System.out.println(allInfo);

        return toString();
    }
}

3 个答案:

答案 0 :(得分:2)

public static String name = CustomerInfoProgram.name;

这仅在初始化字段时设置CustomerInfoProgram.name的值,即一次加载Customer类时 - 在CustomerInfoProgram.name更新时不会更新。

当你调用Customer时,new Customer()类被(最迟)加载,这是在运行任何方法来设置CustomerInfoProgram.name的值之前发生的,所以当你调用它时它是null尝试使用name

如果您想使用CustomerInfoProgram.name的当前值,请直接参考该字段。或者,您可以添加一个getter,它将返回其当前值:

public static String getName() {
  return CustomerInfoProgram.name;
}

然后调用getter代替name

allInfo = (getName() + " " + ...

(完全相同的情况适用于其他领域)。

您还将从StackOverflowError方法获得toString()

public String toString() {

    getAge();
    allInfo = (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
    System.out.println(allInfo);

    return toString();
}

你无条件地在toString()内调用toString(),所以这将一直调用自己,直到它用完堆栈空间。

相反,只需返回allInfo

public String toString() {

    getAge();  // This actually does nothing - do you intend to use it in the return value?
    return (name + " " + address + " " + phoneNumber + " " + dob + " " + age);
}

并在主要方法中调用System.out.println

System.out.println(in); // Implicitly calls toString().

答案 1 :(得分:0)

请参阅@Andy的答案,了解问题的原因。

通常的方法是在创建它之前收集要存储在Customer对象中的值,然后使用非默认构造函数调用。部分示例可能如下所示:

// get individual values here
getCustomerName();
getPhone();
// etcetera

Customer in = new Customer(name, phone, address, dob);

其余的我作为练习留给读者。

答案 2 :(得分:0)

作为静态属性,两个类中的初始化只会是一次。所以发布你得到用户的输入,这些值没有设置为属性。因此无效。