Java Arena
Step 3 of 8

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);
Output
J
a

The 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");
Output
The letter a appears 3 times

This 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");
        }
    }
}
What javac prints
Main.java:4: error: bad operand types for binary operator '=='
        if (word.charAt(0) == "c") {
                           ^
  first type:  char
  second type: String
1 error

One 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));
Output
218
ok

Past 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));
        }
    }
}
Output
c
a
t
The program crashes with
Exception 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().

Your task

Challenge 1 of 3

Fill in the blanks, so the program counts the question marks in a line. For the input Who? What? Why? it prints:

Questions: 3

Challenge 2

Write a method countVowels(String text) that returns how many vowels (a, e, i, o and u, small or capital) the text has. The check runs System.out.println(countVowels("Education")); and expects:

5

Challenge 3

What does this program print? The loop runs backwards.