Haider Abdullah
Haider Abdullah

Reputation: 111

Print my name letter by letter using for loop

How do I print each letter of my name using for loop in Java?

I tried to read tutorials on the Internet but it seems like none of those shows what I'm looking for. What I want to achieve is that once I typed my name it will print the letters one by one alternately.

Upvotes: 0

Views: 1915

Answers (1)

Maciej
Maciej

Reputation: 1994

In java you have String type "YourName" which you can access letter-by-letter using charAt() method

so the code would look like:

String name = "YourName";
for (int i=0; i < name.length; i++) {
    Systerm.out.println(name.charAt(i));
}

This will print every letter in separate line.

Upvotes: 1

Related Questions