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
You cannot declare a method (toLower
) inside another method (main
).
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!
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.
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 ....