Recognizing Syntax Errors

 

When you make syntax errors in your program the complier gives error messages and does not create the bytecode file (.class file).  It saves time and frustration to learn what some of these messages are and what they mean.  Unfortunately, at this stage in the game many of the messages will not be meaningful except to let you know where the first error occurred.  Your only choice is to carefully study your program and examine the error messages.  Compile and run the program to see what it does. Then make the changes below, answering the questions as you go.  You will need the following file: Dialog.class

Answer all these questions on paper and turn it in.

 

1.     Open the file called HelloWorld. java. (This is the traditional first program a computer scientist writes in a new language.)  What does it say when you compile it?  What happens when you run it?

 

 

public class HelloWorld

{

     public static void main(String[ ]  args)

     {

          Dialog.message(“Hello, World!“);

     }

}

 


 

2.     Class name different from file name. Delete the d from the name of the class (i.e. change public class HelloWorld to public class HelloWorl), and compile the program. What was the error message (the error message should be on the bottom.  Don’t forget to include the line number)?

 

 

3.   Misspelling inside string.  Correct the mistake above, then delete the d from the Hello, World in the messages to be printed (change Dialog.message(“Hello, World!“); to Dialog.message(“Hello, Worl!“);  Compile the program. There is no error message – why not ?  Now run the program. What has changed?

 

 

4.  No ending quotation mark in a string literal. Correct the spelling from above.  Delete the last quotation mark from the message (change Dialog.message(“Hello, World!); to Dialog.message(“Hello, World!);  Compile.  What error is printed?

 

 

5.     No beginning quotation mark in a string literal.  Put the ending quotation mark back, and then take out the beginning one (change Dialog.message(Hello, World!“); to Dialog.message(Hello, World!”);.  Compile again.  How many errors this time? (write down how many) Lots, even though there is really only one error.  When you get lots of errors always concentrate on finding the first one listed!!! Often fixing that one will fix the rest. After we study variables the error messages that came up this time will make sense.

 

 

6.  No semicolon after a statement. Fix the last error (put the quotation mark back). Now remove the semicolon at the end of the line that prints the message (change Dialog.message(“Hello, World!“); to Dialog.message(“Hello, World!”).  Compile again. What error message(s) do you get?

 

 

  1.  For this problem, you need to fix the following file so that it will run properly.  It has a few of the errors that you have just learned.  Remember to that if you double click the errors, it will take you to either the line with the error or the line right after the error.  When you get the file to compile and run, get it checked off and turn in your paper with answers 1-6.  BugHunt.java