Characters
A string is a row of characters, and each character has an index, counted from 0, just like the elements of a list or an array. charAt(index) gives the character at an index:
| Index | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
"Java" |
J | a | v | a |
String word = "Java";
char first = word.charAt(0);
char last = word.charAt(word.length() - 1);
System.out.println(first);
System.out.println(last);
J
aThe last index is always length() - 1, one less than the length, because counting starts at 0.
The char type
charAt returns a char: a type that holds exactly one character. A char is written in single quotes, 'a', while double quotes make a String, "a". Like int, char is a primitive type, not an object, so chars are compared with ==:
String word = "banana";
int count = 0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (c == 'a') {
count++;
}
}
System.out.println("The letter a appears " + count + " times");
The letter a appears 3 timesThis loop is the way to go through a string: the index runs from 0 as long as it's less than length(), and charAt(i) gives each character in turn. Other characters work the same way: ' ' is a space and '!' an exclamation mark.
A char and a String are different types, even when they hold the same letter, so comparing one with the other doesn't compile:
public class Main {
public static void main(String[] args) {
String word = "cat";
if (word.charAt(0) == "c") {
System.out.println("Starts with c");
}
}
}
Main.java:4: error: bad operand types for binary operator '=='
if (word.charAt(0) == "c") {
^
first type: char
second type: String
1 errorOne more surprise: a char is stored as a number, its character code, so + between two chars adds their codes. With a String first, + joins as usual:
String word = "ok";
System.out.println(word.charAt(0) + word.charAt(1));
System.out.println("" + word.charAt(0) + word.charAt(1));
218
okPast the end
The indexes of "cat" are 0, 1 and 2. Asking for index 3 crashes the program with a StringIndexOutOfBoundsException. The usual cause is a loop that uses <= where it should use <:
public class Main {
public static void main(String[] args) {
String word = "cat";
for (int i = 0; i <= word.length(); i++) {
System.out.println(word.charAt(i));
}
}
}
c
a
tException in thread "main" java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
at Main.main(Main.java:5)The message says which index was asked for and how long the string is, and the at line points to line 5, the charAt call. The loop condition should be i < word.length().