Page 123 - Introduction to Programming with Java: A Problem Solving Approach
P. 123

                3.22 Strings 89 Since sloth’s last index is 4, not 5, the JVM prints an error message. More specifically, it prints this:
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException:
String index out of range: 5
at java.lang.String.charAt(String.java:558)
at Test.main(Test.java:6)
At first, such error messages are intimidating and depressing, but eventually you’ll
learn to love them. Well, maybe not quite love them, but you’ll learn to appreciate the in-
formation they provide. They provide information about the type of error and where the
error occurred. Try to view each error message as a learning opportunity! At this point,
don’t worry about understanding all the details in the above error message. Just focus on the two callouts and the lines that they refer to.
The above error is an example of a runtime error. A runtime error is an error that occurs while a pro- gram is running, and it causes the program to terminate abnormally. Said another way, it causes the program to crash.
The length Method
The length method returns the number of characters in a particular string. What does this code fragment
    T
T
h
he
e
5
5r
r
e
                Ask: What is computer trying to tell me?
     print?
Apago PDF Enhancer
String s1 = "hi";
String s2 = "";
System.out.println("number of characters in s1 = " + s1.length());
System.out.println("number of characters in s2 = " + s2.length());
Since s1’s string contains two characters (‘h’ and ‘i’), the first print statement prints this: number of characters in s1 = 2
s2 is initialized with the "" value. The "" value is commonly known as the empty string. An empty string is a string that contains no characters. Its length is zero. The second print statement prints this:
number of characters in s2 = 0
In calling the charAt method, you need to insert an argument (an index value) in the method call’s pa- rentheses. For example, animal.charAt(2). On the other hand, in calling the length method, there’s no need to insert an argument in the method call’s parentheses. For example, s1.length(). You may be thinking “With no argument, why bother with the parentheses?” In calling a method, you always need pa- rentheses, even if they’re empty. Without the parentheses, the compiler won’t know that the method call is a method call.
The equals Method
To compare two strings for equality, it’s necessary to step through the characters in both strings and com- pare same-positioned characters, one at a time. Fortunately, you don’t have to write code to do that rather tedious comparison operation every time you want to see if two strings are equal. You just have to call the
i
in
nd
d
e
ex
x;
;i
ef
fe
er
rs
st
t
o
ot
th
he
e
s
s
p
pe
e
c
ci
i
fi
fie
ed
d
 it
ti
is
s
“
“o
o
u
ut
t
o
o
f
fr
r
a
a
n
ng
ge
e.
”
.
”
   T
T
h
he
e
6
6
r
r
e
e
f
fe
e
r
rs
s
t
to
ot
th
he
el
li
i
n
n
e
en
nu
u
m
mb
b
e
e
r
r
i
in
nt
t
h
h
e
e
 p
d
pr
r
o
og
gr
r
a
a
m
m
w
wh
h
e
e
r
r
e
e
t
th
h
e
ee
e
r
r
r
ro
or
r
o
o
c
c
c
cu
u
r
r
r
re
e
d.
.
   121   122   123   124   125