Intent Errors

[ Previous: Run-time Errors | Return to Main Page ]

There is not much that can be said in this section that was not already mentioned in the previous section on run-time errors. The methodology for debugging "intent" errors (your code compiles and does not crash, but it does not do what you intend) is the same as for run-time errors. The only difference is that you do not have a line number from which to begin debugging. Instead, you should place System.out.println() statements at strategic places in your code in order to track down the reason why your program is not running in the way you expect it to.

When trying to debug an intent error, you should ensure that all of the code you expect to be executed actually gets executed. For example, consider the following code:

01  if (this.numPlayers == 8 && !this.isDone)
02  { //...
03  }

If you think that this if statement is actually entered at runtime, it would be worth your while to include the line System.out.println(this.numPlayers == 8 && !this.isDone); before this code block in order to verify that the if clause does indeed evaluate to true.

Also listed on this page are two other topics worth mentioning.

Topics described on this page (click to jump to that topic):

  1. Blank console window
  2. Short-circuiting

Blank console window

If your program runs but does not do anything (there is no output to the console or it just sort of "stalls"), then there is a good chance that your program is stuck in an infinite loop somewhere. This usually occurs with while loops, but can happen with a for loop in some cases as well.

For each while loop in your code, do the following:

01  while (i >= 0)
02  {
03     System.out.println("in loop");
04     //...
05  }

You will know that you are stuck in an infinite loop if your console output looks something like the following:

in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
in loop
[...and so on, without seeming to stop]

To debug this problem, ensure that the while clause will eventually evaluate to false somewhere within the body of the loop.


Short-circuiting

Remember that Java does not evaluate an entire compound boolean expression if it does not have to, using a method called short-circuit evaluation, described in the table below.

Scenario Java's Short-circuit Evaluation Rules
//say A and B are boolean expressions
if (A && B)
{ //...
}

If A is true, then B is evaluated.

If A is false, then B is not evaluated.

//say A and B are boolean expressions
if (A || B)
{ //...
}

If A is true, then B is not evaluated.

If A is false, then B is evaluated.

If your program contains compound boolean expressions, then you may wish to print out the value of each part to ensure that it is being evaluated in the way you intend. For example:

01  System.out.println(x >= 0);  
02  System.out.println(y == 7);
03  System.out.println(z < x + y);
04  if (x >= 0 && y == 7 || z < x + y)
05  { //...
06  }

Return to Main Page
Created by Terry Anderson (tanderso at uwaterloo dot ca) for use by ISG, Spring 2005