You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
50 CHAPTER 2. BASICS<br />
method charAt(...) returns the character at a specific position to the point<br />
where the method is invoked. The method charAt(...) must always have<br />
an argument value (the position) passed to it. For <strong>Java</strong>, positions <strong>with</strong>in a<br />
string begin at 0 and so the first hyphen should be at position 3. Note in<br />
the program, at line 9, shown here:<br />
char firstHyphen = sin.charAt(3);<br />
how the method is invoked (a period separates the name of the string, sin,<br />
from the name of the method) and how the value 3 is passed to the method<br />
(in parentheses as an argument value).<br />
Listing 2.10: Obtaining a character <strong>with</strong>in the string at a specific position<br />
1 public class UsingStringCharAt<br />
2 {<br />
3 public static void main(String[] args)<br />
4 {<br />
5 // variable password is of type String<br />
6 // variable passwordLength is of type int<br />
7 String sin = "123-987-555";<br />
8 // use the charAt() method<br />
9 char firstHyphen = sin.charAt(3);<br />
10 // Display the string and<br />
11 // the character in position 3<br />
12 System.out.println("SIN is");<br />
13 System.out.println(sin);<br />
14 System.out.println("character at position 3<br />
is");<br />
15 System.out.println(firstHyphen);<br />
16 }<br />
17 }<br />
Figure 2.6: Showing the string and the character at position 3