user3342648
user3342648

Reputation: 13

Read input string

I'm just new in Java and I would like to ask if it's possible to separate each letter of a word. For example, my input is "ABCD" and I would like to display each letter of that word. So, the expected output should be. The input word consists of letters: "A", "B", "C", "D". Sorry, I am just very new in Java and I would like to know if this is possible.

Upvotes: 0

Views: 126

Answers (4)

Kakarot
Kakarot

Reputation: 4252

Its possible to split your String into individual characters. Below is the method to do it :

String str = "ABCD";

// this will create Array of all chars in the String
char[] chars = str.toCharArray(); 

// Now loop through the char array and perform the desired operations
for(char val : chars)
{
  // do something
  // variable val will have individual characters
}

Upvotes: 2

dijkstra
dijkstra

Reputation: 1078

You can use String methods. (length() and charAt(int index)) I think you are new at programming so using String methods are better for you if you do not know arrays.

Upvotes: 0

Caffeinated
Caffeinated

Reputation: 12484

Yes, you use the String.toCharArray() method ( here's the API ).

Learn more here - String to char array Java

example:

String happy = "Yaya happee";
char[] happier = happy.toCharArray();

Upvotes: 1

Mason T.
Mason T.

Reputation: 1577

String.toCharArray()

is the method you are looking for

It will create an Array of Chars. So your string would become

{'A','B','C','D'}

Upvotes: 2

Related Questions