-2

I have made a method which random the string array and print it out. But now I need to use that method and assign it to a string to count the number of letters in the string. I cant seem to find the answer anywhere. Is there any way that I can do? Help me out. I m a beginner in programming.

Austin
  • 55
  • 1
  • 8

1 Answers1

0

You can use Arrays.toString() in combination with StringBuilder/StringBuffer to iterate over the array and keep on appending the array element to this string object .

EX:

StringBuilder stringBuilder = new StringBuilder();
   for(String string : array) {
   stringBuilder.append(string);
 }
 return stringBuilder.toString();

For counting the no. of letters you can use:

String input = userInput.toLowerCase();// Make your input lower case.
int[] alphabetArray = new int[26];
for ( int i = 0; i < input.length(); i++ ) {
     char ch=  input.charAt(i);
     int value = (int) ch;
     if (value >= 97 && value <= 122){
     alphabetArray[ch-'a']++;
    }
}

And for displaying the result:

 for (int i = 0; i < alphabetArray.length; i++) {
  if(alphabetArray[i]>0){
    char ch = (char) (i+97);
    System.out.println(ch +"  : "+alphabetArray[i]);   //Show the result.
  }         
 }
Odin
  • 580
  • 11
  • 24