Java Reference
In-Depth Information
System.out.print("Enter an integer: ");
num = console.nextInt();
System.out.println();
System.out.println("num: " + num);
System.out.print("Enter first name: ");
name = console.next();
System.out.println();
System.out.print("Enter height: ");
height = console.nextDouble();
System.out.println();
System.out.println("Name: " + name);
System.out.println("Height: " + height);
}
}
As you can see, this program is easier to read. Your programs should be properly indented
and formatted. To document the variables, programmers typically declare one variable
per line. Also, always put a space before and after an operator.
Avoiding Bugs: Consistent, Proper Formatting
and Code Walk-Through
Java is a free-format language in the sense that programming instructions need not be typed
in specific columns. For example, you can declare one or more variables in a line and input
and/or output statements can follow the declarations of variables as illustrated in the first
program given in Example 2-27, which is an improperly formatted program. The compiler
will have no trouble compiling this program. However, for us, this program is very hard to
follow and if there are syntax or semantic (logical) errors, it will be very tedious and
tiresome to debug this program. The second program in Example 2-27 is properly
formatted and easier to read and follow. As you will discover, consistent and proper
formatting will make it easier to develop, debug, and maintain programs. Throughout
the topic, you will see consistent and predictable use of blanks, tabs, and newline characters
to separate the elements of a program. For example, we have indented statements four
spaces to the right within a block (that is, between { and } .) Rather than four spaces, you
can indent statements three spaces to the right; the important thing is to be consistent. We
will list a few more indenting rules when we introduce selection and looping structures in a
program.
Examples 2-14 and 2-18 illustrate how to walk-through a program. When you write
programs unintentional typos and errors are unavoidable. The Java compiler will find the
syntax rules and give some hints how to correct them. However, the compiler may not
DEBUGGING
 
 
Search WWH ::




Custom Search