Void methods cannot return a value

前端 未结 10 2183
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-23 01:38

I\'m following the CS106A lectures online. I\'m going through the code on Lecture 12, but it\'s giving me errors in Eclipse.

This is my code. It seems the error is beca

相关标签:
10条回答
  • 2021-01-23 01:43

    nested method defining is not allowed in java .You have defined your method toLower() inside main method

    0 讨论(0)
  • 2021-01-23 01:44

    string[] args in main method is used to follow the prototype defined by the technology. If you dont used it then JVM will considered it as a simple method. Also as this is prototype then remember technology used 0 byte of an array. for ex:

                   class MainDemo{
                        public static void main(){
                        System.out.println("I am main method without arg");
                    }
                    public static void main(String[] args){
                            main();   //As method is static so no need to create an object
                        System.out.println("MainDemo");
                    }
                    }
    

    O/p for above will give:

     I am main method without arg 
     MainDemo
    

    Regarding your code, you cannot define/declare any method inside main method. Use it outside main method but within class.

    0 讨论(0)
  • 2021-01-23 01:44

    Yes void return type can not return any value.

    It will be better if you create a separate function for this process which will return some value and call it from main().

    public class Test
    {
        public static void main(String[] args)
        {
            String[] a = testMethod();
        }
    
        public String[] testMethod()
        {
            .....
            .....
            return xx;
        }
    }
    

    Hope it will help you.

    Thanks

    0 讨论(0)
  • 2021-01-23 01:49

    You can't have nested method in java.

    toLower() method is inside main().

    separately use both method. void means there isn't any return from those method.

    You can try as follows

    public static void main(String[] args){
       char output=new CS106A().toLower('a'); // calling toLower() and take the
                                              // return value to output
    
    }
    
    
    public char toLower(char ch){
    if (ch >= 'A' && ch <= 'Z'){
        return ((ch - 'A') + 'a');
      }
     return ch;   
    }
    
    0 讨论(0)
  • 2021-01-23 01:52

    Because You have added ; at the end of an method

    corrected Code:

    public class CS106A {
    
    public static void main(String[] args){
    Char char=new CS106A.toLower('s');
    System.out.println(char);
    }
    
    public char toLower(char ch)
    {
        if (ch >= 'A' && ch <= 'Z'){
            return ((ch - 'A') + 'a');
        }
        return ch;      
    }
    }
    

    Please Read how to write Methods in java on any java website

    0 讨论(0)
  • 2021-01-23 01:57
    public class CS106A
    {
        public static char toLower(char ch)
        {
            if (ch >= 'A' && ch <= 'Z')
            {
                return ((ch - 'A') + 'a');
            }
        return ch;
        }
        public static void main(String[] args)
        {
    
           System.Out.Println(toLower('A'));
    
        }
    
    }
    

    Hope this will help you.

    0 讨论(0)
提交回复
热议问题