简体   繁体   English

在另一个void函数内部传递一个私有void函数(编译错误Java)

[英]passing a private void function inside another void function (compile error java)

Let's say I have a function which makes the names into first letter uppercase and the rest in lowercase when a parameter is entered. 假设我有一个函数,当输入参数时,该函数使名称首字母大写,其余字母小写。

I have another function to system print other things including the function I mentioned above. 我还有另一个功能可以系统打印其他内容,包括我上面提到的功能。 But I keep on getting compile error saying 'void' type not allowed here. 但是我继续收到编译错误,说这里不允许使用“ void”类型。

Still really new with Java, not sure how I can adjust it. Java仍然是真的很新,不确定如何调整它。 Can someone give me a hand? 有人可以帮我吗?

This is the first function I mentioned 这是我提到的第一个功能

private void makePrettyString(String modelName)
    {

        // Change first letter of the name into upper case and store the first char only
        char first = Character.toUpperCase(modelName.charAt(0));

        // lower case the whole name
        String lower = modelName.toLowerCase();

        // store the whole name except the first char into the variable
        String restInLowerCase = lower.substring(1);

        // Combine the first char (which is upper case) and the rest of the name (which is in lower case)
        this.modelName = first+restInLowerCase; 
    }

The second function to get some details 第二个功能获得一些细节

public void getCarDetails()
{
    System.out.println(makePrettyString(modelName));
}

Thank you all for the help! 谢谢大家的帮助! I just realized this is all I need to do change the method from void to String and of course as everyone says return something So this works 我只是意识到这就是我需要做的所有事情,将方法从void更改为String ,当然每个人都说返回了一些东西,所以这可行

private String makePrettyString(String modelName)
{

    // Change first letter of the name into upper case and store the first char only
    char first = Character.toUpperCase(modelName.charAt(0));

    // lower case the whole name
    String lower = modelName.toLowerCase();

    // store the whole name except the first char into the variable
    String restInLowerCase = lower.substring(1);

    // Combine the first char (which is upper case) and the rest of the name (which is in lower case)
    return = first+restInLowerCase; 
}

Modify the function as: 将该函数修改为:

private String makePrettyString(String modelName) {

   //Your existing code

   return this.modelName;
}

With the above your job gets resolved as the udner the System.out.println(fun-call) the argument is expecting some value as written which unfortunately is nothing but void!! 有了以上这些,您的工作就可以通过System.out.println(fun-call)得到解决,该参数期望所写的值是可惜的!

change void of first method to string. 将第一种方法的无效更改为字符串。 it has to return a string so change the method signature to string 它必须返回一个字符串,因此将方法签名更改为字符串

like this : 像这样 :

private String makePrettyString(String modelName)     // <-- change has to be made here  (String instead of void)
{

    // Change first letter of the name into upper case and store the first char only
    char first = Character.toUpperCase(modelName.charAt(0));

    // lower case the whole name
    String lower = modelName.toLowerCase();

    // store the whole name except the first char into the variable
    String restInLowerCase = lower.substring(1);

    // Combine the first char (which is upper case) and the rest of the name (which is in lower case)
    return = first+restInLowerCase; 
}

The line --> System.out.println(makePrettyString(modelName)); 该行-> System.out.println(makePrettyString(modelName)); will cause the error because the class PrintStream in which println() is defined doesn't have an overloaded method of println() which takes void as argument. 会导致错误,因为在其中定义了println()的类PrintStream没有以void为参数的println()的重载方法。

Check all overloaded methods here . 在此处检查所有重载方法。

Just to clear the mist note that you cannot have a method that accepts void as argument in Java. 只是为了澄清薄雾,请注意,在Java中,您不能拥有将void作为参数接受的方法。 Now, there is a place holder / wrapper for void which goes by the name Void which *also needs an argument of type Void . 现在,有一个用于void占位符/包装 ,其名称为Void *还需要一个Void类型的参数。

If both methods getCarDetails() and makePrettyString() are in the same class, 如果两个方法getCarDetails()makePrettyString()都在同一类中,

Simply write 只需写
makePrettyString(); System.out.println(this.modelName) makePrettyString(); System.out.println(this.modelName) ; makePrettyString(); System.out.println(this.modelName) ;

Instead of using a parameter makePrettyString(String modelName) , you can access this.modelName in the makePrettyString() method. 如果不使用参数的makePrettyString(String modelName)您可以访问this.modelNamemakePrettyString()方法。

HTH. HTH。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM