"空隙"这里不允许输入 - 我是编码的新手

时间:2014-10-10 00:07:29

标签: java void

对于public void mailingLabel()方法,我不断收到错误,告诉我不允许空格。当我将其更改为String并修改代码以包含return语句时,它工作正常。有什么问题?

public class StreetAddress {

    private String myStreet;
    private String myCity;
    private String myState;
    private String myZIP;

    public StreetAddress(String street, String city, String state, String zip) {
        myStreet = street;
        myCity = city;
        myState = state;
        myZIP = zip;
    }

    public void mailingLabel() {
        System.out.println(getStreet());
        String p = getCity() + ", " + getState() + " " + getZIP();
        System.out.println(p);
    }

    public String getStreet() {
        return myStreet;
    }

    public String getCity() {
        return myCity;
    }

    public String getState() {
        return myState;
    }

    public String getZIP() {
        return myZIP;
    }

    public void setStreet(String street) {
        myStreet = street;
    }

    public void setCity(String city) {
        myCity = city;
    }

    public void setState(String state) {
        myState = state;
    }

    public void setZIP(String zip) {
        myZIP = zip;
    }
}

1 个答案:

答案 0 :(得分:3)

我必须猜测你的问题似乎不完整,但话说回来,我的猜测是你试图将mailingLabel()方法调用传递给println方法,这是不可能的:< / p>

StreetAddress someAddress = new StreetAddress(...);
System.out.println(someAddress.mailingLabel());

编译器会抱怨你正在尝试打印一个空白,这是有道理的。相反,要么让方法返回一个String,要么保持方法不变,只是自己调用它而不是println。即,

StreetAddress someAddress = new StreetAddress(...);
someAddress.mailingLabel();

顺便说一句,考虑给StreetAddress一个public String toString()方法覆盖,返回一个String,其中包含描述对象状态的相关数据。