get no by character in java code example
Example 1: counting the number of characters in a string java
public static void main(String[] args) {
int wordCount = 0;
String word = "hill";
for (char letter = 'a'; letter <= 'z'; letter++) {
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == letter) {
wordCount++;
}
}
if (wordCount > 0) {
System.out.println(letter + "=" + wordCount);
wordCount = 0;
}
}
}
Example 2: How do you count characters in a string array in Java?
package practical5;
import java.util.Arrays;
public class Part1_9 {
public static void main(String[] args) {
String quoteArray[] = { "\"Continuous", "effort", "not", "strength",
"nor", "intelligence", "is", "the", "key", "to", "unlocking",
"our", "potential.\"\n" };
for (int counter = 0; counter < quoteArray.length; counter++) {
System.out.print(quoteArray[counter] + " ");
}
for (String element : quoteArray) {
System.out.print(element + " ");
}
System.out.println();
System.out.println("Number of words in array: " + quoteArray.length);
**
for (int counter = 0; counter < quoteArray.length; counter++) {
String letters = new String(quoteArray[counter]);
}
}
}