Java Reference
In-Depth Information
10.
int tempNum;
11.
12.
num = 18;
13.
14.
2
tempNum = 2 * num;
15.
16.
System.out.println("Num = " + num + ", tempNum = " - tempNum);
17.
}
18.
}
When this program is compiled, it generates the following error:
ProgramNum3.java:16: operator - cannot be applied to java.lang.String,int
System.out.println("Num = " + num + ", tempNum = " - tempNum);
^
1 error
It specifies that the error is in Line 16 and it indicates that the operator - cannot be
applied to strings. Recall that to join two strings, we use the operator + . So in Line 16, we
must replace - with + at the place indicated by ^ . After correcting this error and renaming
this program, the program is:
1.
import java.util.*;
2.
3.
public class ProgramNum4
4.
{
5.
static Scanner console = new Scanner(System.in);
6.
7.
public static void main(String[] args)
8.
{
9.
int num;
10.
int tempNum;
11.
12.
num = 18;
13.
14.
tempNum = 2 * num;
15.
16.
System.out.println("Num = " + num + ", tempNum = " + tempNum);
17.
}
18.
}
When we compile this program, the compiler will not generate any syntax errors and it will
create the file ProgramNum4.class , which can be executed using the appropriate Java
command.
When we execute this program it will generate the following output:
Num = 18, tempNum = 36
As you learn Java and practice writing and executing programs, you will learn how to
spot and fix syntax errors. It is possible that the list of errors reported by the compiler is
Search WWH ::




Custom Search