1

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 because of the word void in my main method. I tried deleting the main method, but of course Java can't run without it.

I'm a newbie and no one has explained what the String[] args thing really means, but I've been told to just ignore it and use it. I'd appreciate if someone could explain that to me as well.

This errors also comes up on the 'toLower' method; no idea what it means: Illegal modifier for parameter toLower; only final is permitted

(if it helps; the point of the code is to convert an uppercase letter to a lowercase one)

public class CS106A {

    public static void main(String[] args){

        public char toLower(char ch);
            if (ch >= 'A' && ch <= 'Z'){
                return ((ch - 'A') + 'a');
        }
        return ch;      
    }

}

Thanks

Syjin
  • 2,740
  • 1
  • 27
  • 31
Aaron Ausmus
  • 53
  • 1
  • 1
  • 6
  • 1
    Similar type of problem was discussed 50 minutes back http://stackoverflow.com/questions/20489856/java-illegal-start-of-an-expression-error – SpringLearner Dec 10 '13 at 09:45
  • The `String[] args` is an array of the command line arguments you passed your program. Most likely this is empty as you didn't pass any. – Peter Lawrey Dec 10 '13 at 09:48

10 Answers10

6

You should be defining your method outside of main, like:

public class YourClass
{
    public static void main(String... args)
    {

    }

    public char yourMethod()
    {
         //...
    }
}

Java does not support nested methods; however, there are workarounds, but they are not what you're looking for.

As for your question about args, it is simply an array of Strings that correspond to command line arguments. Consider the following:

public static void main(String... args) //alternative to String[] args
{
    for (String argument: args)
    {
        System.out.println(argument);
    }
}

Executing via java YourClass Hello, World!

Will print

Hello,
Word!

Community
  • 1
  • 1
Steve P.
  • 14,489
  • 8
  • 42
  • 72
2

You cannot declare a method (toLower) inside another method (main).

BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
1

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

Gourav
  • 813
  • 2
  • 10
  • 23
1

You need to declare your method outside of the main

public class YourClass
{
    public static void main(String... args)
    {

    }

    public char yourMethod()
    {

    }
}

the string args bit is so when you run it through command line you can send values (as strings)

>java myprogram var1 var2 ....
Melbz
  • 512
  • 4
  • 14
1

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

Sangam Belose
  • 4,262
  • 8
  • 26
  • 48
0

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;   
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

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

Deepak
  • 2,287
  • 1
  • 23
  • 30
0

You cannot have such nested methods in Java. CS106A is a class. main() and toLower() are two methods of it. Write them separately.

As for String[] args in the main() method argument it is similar to saying int arc, char **argv in C if you have learned it before. So basically args is an array where all the command line arguments go.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
0
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.

Muhammad Essa
  • 142
  • 1
  • 10
0

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.

Tushar Goel
  • 35
  • 2
  • 6