Join Peggy Fisher for an in-depth discussion in this video Debugging logic and syntax errors, part of Learning Java 8.
- In programming, there are two main types of errors that we will review, syntax errors and logic errors. A syntax error occurs when we make a mistake in our coding, such as forgetting a semicolon to indicate the end of a statement. A logic error is harder to find. This occurs when we have all the correct syntax but we coded a portion of the program with an error, such as maybe, divide by zero. This program has several syntax and logic errors. Let's fix them. As you can see on line 22, there's a red underscore.
If I put my mouse over the exclamation point in the left-hand column, it says it's expecting a semicolon. These are good hints. I see, I forgot the semicolon here. Once I put that in there, the red line will go away. The next error, let's go ahead and see what that is. When I mouse over it, it gives me a message. It says incompatible types possible lossy conversion from double to int. We mentioned this when we talked about data conversions using implicit and explicit conversion.
I cannot do a narrowing conversion where my value is a double and my number is an integer. I cannot put a double value into an integer variable. Let's use an explicit conversion to fix this. I'm gonna put the word int, in parentheses, in front of the variable value. There. Now I've changed the value to just three and I can put the value three into the variable number. At this point, I've corrected all of my syntax errors.
There's no more red and I'm ready to run the program. Let's give it a try. Woops, as you can see at the bottom, I have a big red error message. Exception in thread "main" java.lang.ArithmeticException. And it tells me that they have a problem where I'm trying to divide by zero. If we look at our program, I need to scroll down a little bit, you'll see line 24 has divide by zero. It did not cause a syntax error but logically we know we can't divide by zero. I'm gonna comment this line out, because it just doesn't make sense to have that in there.
Ok, let's try it again. This time I got all the way down to the line that prints out the first day of the week using the array daysOfTheWeek. It printed out the word Monday. And then below that, I have another red exception error. And this one says java.lang.ArrayIndexOutOfBoundsException. And it tells me the number that's out of bounds. Ok let's look at our program. So the first day of the week is, daysOfTheWeek at index one.
Well if you remember my brief discussion on arrays, I said that arrays always start with an index value of zero. So let's change this to a zero. And since there's seven days of the week, if I start with zero, the last day is actually going to be day six. Let me scroll down a little bit and now let's try it again. Alright, the first day of the week is Sunday, and if you look at my array you'll see that Sunday is the first day in the array initializer list, and Saturday is the last day, so that's working.
Then I calculate the average of three plus five plus eight divided by three. As you know from our prior example, when you add three, five and eight and divide by three, you expect to get 5.33 repeating. But if you look at our program, it actually gives us a value of 10.66 repeating. As you can see, this does not cause the program to abruptly end, but it does give us an incorrect value. If you look at line 27, you can probably quickly identify what happened.
It's dividing eight by three first, and then it's adding five, and then adding three. This is not really what we wanted to happen. So we have to put parentheses around the part of the program that we want to add first, and then divide the total by 3.0. Ok, I'm gonna run it one last time. That looks much better. The first day of the week is Sunday, the last day is Saturday, and when I add three, five and eight, and divide by three to get the average, I get 5.33.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs