i'm having a hard time creating a program that will display the initials of a string provided by the user. The program should also display the number of words of a given string. i've tried this code, but the problem is, the program would only display the first initial and would only count one word.
this is the code:
import java.util.Scanner;
public class word_counter {
public static void main (String [] args){
Scanner in = new Scanner (System.in);
String statement = " ";
int counter = 0;
System.out.print ("Enter a String: ");
statement = in.nextLine ();
statement.length();
System.out.println ("Initial of the String: ");
for (int i = 0; i < statement.length(); i++){
if ((statement.charAt (i) != ' ') && (i == 0)) {
System.out.println (statement.charAt (i) + " ");
counter ++;
}
}
System.out.print ("Word Count: " + counter);
}
}