Run time errors
Ok this is an exercise I have been given to learn about run time errors. The program I have been given has purpose mistakes so that the program compiles but doesn't run. I need to explain the error and fix it accordingly. This is how it looks
001: public class Quote
003: public void Main(String args)
005: System.out.println("Programming is about making the stupid seem clever.");
006: System.out.println(" - ^ - ");
007: System.out.println( 0 / 0 );
008: System.out.println(" = ");
009: System.out.println(" (At least, to the dumb user!) ");
The run time error says:
Exception in thread "main" java.lang.NoSuchMethodError: main
Does this mean the program is missing a main method? (as the code says "Main" rather than "main")
Changes I have made to fix it:
003: public static void main(String args)
007: System.out.println(" 0 / 0 ");
The program now works but are there any parts i have added that arn't required?
e.g. do i need the word "static" in there? If so why?
very new to this stuff if you didn't guess ;)
*the correct spacing hasn't come out properly in the post, im also new to the forum ;)*
Yep you added the required main method.
You do need that static method there. The main method is rather special because you don't actually invoke it yourself. The guy who calls it (the JVM) wants it that way so he can find it and call it easily without needing to worry about creating an instance of your class.
So what about the square brackets after "String". I know they are required because i tried the program without them. But why do they actually do? And what happens when i add the quotations on line 7? thanks for helping me
Without the brackets you just have a string, with the brackets you have an array of strings. So for 'String args' you have an array called 'args', that is args, args ... (Well, the size for the Main method depends on how many arguments you actually have when starting the program). For further info on arrays see
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
You mean the size of the args array instead?
Originally Posted by pellebye
And the quotations make that everything between the quotes is shown literally on the screen except for escape characters.