Error Message Name:
Error: ')' expected
Example:
System.out.println(89 "years old");
Solution: Note that println takes in a single String as a parameter. A "+" sign is needed to concatenate integers, doubles, or any variable to a String. In the above example, we need to change the code to:
System.out.println(89 + "years old");
Error Message Name:
Error: unclosed string literal
Example:
String s = "This is gonna be one long sentence containing 1430847309537409547309354 words";
Cause: The Java Compiler does not allow you to break down a long String onto more than one line without using proper concatenation operator and quotation marks.
Solution: Every line of String must be closed off with a quotation mark. Any new line of text is concatenated to the existing String using the "+" operator. In the above example, we should have:
String s="This is gonna be one long sentence containing + "1430847309537409547309354" + "words";
Error Message Name:
StringIndexOutOfBoundsException: String index out of range: {number}
Example:
1)
String output = "Hello World!";
System.out.println("The output is " + output.substring(15, 20));
This would give the following error message:
StringIndexOutOfBoundsException: String index out of range: 20
2)
String output="Hello World!";
System.out.println("The output is "+output.substring(5, 3);
This would give the following error message:
StringIndexOutOfBoundsException: String index out of range: -2
Cause: One or both of the parameters are violating rules set by the String class. If {number} is positive in the error message, then one or both of your parameters is larger than the length of the String. If {number} is negative, it can be two things. Either one or both parameters are less than zero, or your second parameter is larger than your first parameter.
Solution: Make sure that the parameters follow these rules:
1) They are both bigger than or equal to zero, and less than the length of the String.
2) The first parameter is less than or equal to the second parameter.
Tip: The number displayed in the error message should help you determine if you have the "off by one" bug. If {number} is the length of the String, or -1, then you likely have this bug. To fix this, simply increment or decrement your parameter by one.
Description: The program is not outputting any of the escape characters.
Example: Suppose you want to print out "CS125", then on a new line, "Section 101". Your code might be:
System.out.println("CS125 /n Section 101");
This would produce the following output:
CS125 /n Section 101
Cause: This is due to the backslash you are using.
Solution: To use the escape characters correctly, you should be using " \ " as your slash and not " / " :
System.out.println("CS125 \n Section 101");