构造函数错误(JAVA)

时间:2015-04-29 17:34:18

标签: java constructor

所以我能够让我的第一个构造函数工作,但不是我的第二个构造函数。对于p2我试图显示(字符串,字符串,字符串,双精度,双精度,字符串,字符串)。因为它代表显示错误"字符串不能转换为double",但我不知道我在做什么。

这是我的主要代码。         包studenthealthservices;

    public class Studenthealthservices {

    public static void main(String[] args) {

        EMR p1 = new EMR("Colin","10-22-74");
        System.out.printf(p1.toString());

        EMR p2 = new EMR("Anquan","9-30-77", "stomach ache","98", "120", "stress", "Tylenol");
        System.out.printf(p2.toString());

    }}

这是我的EMR课程的代码         包studenthealthservices;

public class EMR {

    private String name;
    private String dob;
    private String rfv;
    private double bodyt;
    private double hr;
    private String diag;
    private String pmeds;

    public void setName(String name) {
        this.name = name;
    }
    public EMR (String name, String dob) {
    this.name = name;
    this.dob = dob;
}
    public String getName(){
        return name;
    }

    public EMR (String name, String dob, String rfv, double bodyt, double hr,          String diag, String pmeds){
        this.name = name;
        this.dob = dob;
        this.rfv = rfv;
        this.bodyt = bodyt;
        this.hr = hr;
        this.diag = diag;
        this.pmeds = pmeds;

    }
    public String getDob() {
        return dob;
    }

    public void setDob(String dob) {
        this.dob = dob;
    }

    public String getRfv() {
        return rfv;
    }

    public void setRfv(String rfv) {
        this.rfv = rfv;
    }

    public double getBodyt() {
        return bodyt;
    }

    public void setBodyt(double bodyt) {
        this.bodyt = bodyt;
    }

    public double getHr() {
        return hr;
    }

    public void setHr(double hr) {
        this.hr = hr;
    }

    public String getDiag() {
        return diag;
    }

    public void setDiag(String diag) {
        this.diag = diag;
    }

    public String getPmeds() {
        return pmeds;
    }

    public void setPmeds(String pmeds) {
        this.pmeds = pmeds;
    }

    @Override
    public String toString(){
        return"name :" + this.name +"Date of Birth:" + this.dob;
    }
}

4 个答案:

答案 0 :(得分:4)

"98"String,而不是double。您需要删除引号:

 EMR p2 = new EMR("Anquan","9-30-77", "stomach ache", 98, 120, "stress", "Tylenol");

答案 1 :(得分:3)

当你将双打传递给方法(即构造函数)时,你不能用引号括起double,否则它们就是字符串。

答案 2 :(得分:1)

在这一行

EMR p2 = new EMR("Anquan","9-30-77", "stomach ache","98", "120", "stress", "Tylenol");

正在调用此构造函数

public EMR (String name, String dob, String rfv, double bodyt, double hr,          String diag, String pmeds)

值“98”和“120”都是字符串,而不是参数 bodyt hr 的双倍。

因此,请将此行更改为

EMR p2 = new EMR("Anquan","9-30-77", "stomach ache",98d, 120d, "stress", "Tylenol");

在这种情况下,双精度的后缀d是可选的。你可以使用98和120。

答案 3 :(得分:1)

当您在对象P2的EMR的构造函数中传递值时,您将在“”内传递98和120。

EMR p2 = new EMR("Anquan","9-30-77", "stomach ache","98", "120", "stress", "Tylenol");

这些是双值,不应在双引号内传递。将代码更改为

EMR p2 = new EMR("Anquan","9-30-77", "stomach ache",98, 120, "stress", "Tylenol");