Skip to content

Commit 2a7c18d

Browse files
nshizirungudieumerci@gmail.comnshizirungudieumerci@gmail.com
authored andcommitted
Merge branch 'master' into chapter9.4
2 parents f52b610 + f245fe6 commit 2a7c18d

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

source/ch9_commonmistakes.ptx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<p>
6464
A common mistake in Java is to forget that every statement must end with a semicolon (<c>;</c>).
6565
</p>
66+
<listing xml:id="semicolon-error">
6667
<program language="java" interactive="activecode" line-numbers="yes">
6768
<code>
6869
// Histo.java
@@ -77,8 +78,9 @@
7778
}//End of class
7879
</code>
7980
</program>
81+
</listing>
8082
<p>
81-
The error "';' expected" on line 7 of <c>Histo.java</c> means that a semicolon is missing at the end of the statement <c>Scanner data = null</c>. In Java, every statement must be terminated with a semicolon (<c>;</c>) to indicate its completion. The arrow points to <c>null</c> because that's where the compiler expected to find the semicolon.
83+
The error "';' expected" on line 7 of <c>Histo.java</c> in <xref ref="semicolon-error" text="type-global"/> means that a semicolon is missing at the end of the statement <c>Scanner data = null</c>. In Java, every statement must be terminated with a semicolon (<c>;</c>) to indicate its completion. The arrow points to <c>null</c> because that's where the compiler expected to find the semicolon.
8284
</p>
8385

8486
</section>
@@ -87,8 +89,9 @@
8789
<title>Forgetting to declare your variables</title>
8890
<p>
8991
In Python, you can use a variable without declaring it first, but in Java, you must declare all variables before using them.
90-
If you try to use a variable that has not been declared, the Java compiler will give you an error message like this:
92+
<xref ref="undeclared-variable" text="type-global"/> shows If you try to use a variable that has not been declared, the Java compiler will give you an error message like this:
9193
</p>
94+
<listing xml:id="undeclared-variable">
9295
<program language="java" interactive="activecode" line-numbers="yes">
9396
<code>
9497
import java.util.ArrayList; // Import necessary class
@@ -108,6 +111,7 @@
108111
} // End of class
109112
</code>
110113
</program>
114+
</listing>
111115
<p>
112116
The 'cannot find symbol' error for the variable <c>count</c> on line 6 indicates that <c>count</c> was used before it was declared within the <c>Histo</c> class. In Java, all variables must be explicitly declared with a data type (e.g., <c>int</c>, <c>String</c>, <c>ArrayList&lt;Integer&gt;</c>) before they can be assigned a value or referenced in any way. The arrow in the error message points to where the undeclared variable <c>count</c> was first encountered. To resolve this, <c>count</c> needs to be declared with its appropriate type (e.g., <c>ArrayList&lt;Integer&gt; count;</c>) before any attempt to initialize or use it.
113117
</p>

0 commit comments

Comments
 (0)