How to traverse a string in Java?

To iterate through a string in Java, call String.charAt in a loop over String.length.

Here's how you do it:

String string = "Hello World!";

for (int i = 0; i < string.length(); i++) {
  System.out.println(string.charAt(i));
}

/*
This loop will print:
H
e
l
l
o

W
o
r
l
d
!
*/