Post on 17-May-2015
transcript
Common Errors and Solutions
CST200 – Week 1: Common Errors when Starting to write and compile Java Code
Instructor: Andreea Molnar
Outline
•Disclaimer
•Names
•Brackets
•Semicolons
Disclaimer
These are common errors I have seen as a teaching assistant, and are based solely on my experience.
Everyone is different, therefore:
• some of you will not make these mistakes
• others will not make these mistakes after seeing this presentation
• For others it may be a good resource to consult
Error Category
Names
Names (e.g. class names, variable names, method names) are a single string!
public class Hello World {
}
Is the class name Hello or World?
public class Hello World {
}
After the first string –Hello – a bracket is
expected.
public class Hello World {
}
public class Hello World {
}
public class HelloWorld {
}
public class Hello {
}
public class World {
}
Keep in mind, it is not only class names, variable names and method names have to be composed of a single connected string.
public class HelloWorld {
public static void main(String [] args) {
int Britteny Spears;
}
}
public class HelloWorld {
public static void main(String [] args) {
int Britteny Spears;
}
}
After the first string –Brittney
– a ; is expected.
public class HelloWorld {
public static void main(String [] args) {
int Britteny Spears;
}
}
public class HelloWorld {
public static void main(String [] args) {
int BrittenySpears;
int Britteny;
int Spears;
}
}
public class HelloWorld {
public static void main(String [] args) {
//this is the best
int brittenySpears;
int britteny;
int spears;
}
}
Brackets
Every open bracket has to be closed in the program!!!
As a rule of thumb check that every bracket you open is closed somewhere in the program: (), [], {}.
For the purpose of this class use curly brackets –{}- at the beginning and the end of a class and a method (more about these in the next week).
Every open bracket has to be closed in the program!!!
public class HelloWorld {
public static void main(String [] args) {
System.out.println(“Hello World”);
}
}
public class HelloWorld {
//beginning of the class body
public static void main(String [] args) {
System.out.println(“Hello World”);
}
} //end of the class body
public class HelloWorld {
public static void main(String [] args) {
//beginning of the method body
System.out.println(“Hello World”);
} //end of the method body
}
public class HelloWorld
public static void main(String [] args) {
System.out.println(“Hello World”);
}
}
Bracket missing
Bracket expected
Enjoy finding the other errors.
Semicolons
You have to learn to love ; in Java. Every statement in Java ends with a ;.
public class HelloWorld {
public static void main(String [] args) {
System.out.println(“Hello World”);
}
}
Summary
•Names are a single string.
•Brackets always come in pairs.
•Semicolons end a statement.
Thank you