JAVA- Store output into char array

Newbie question: Created very basic program to read user input and Sys.out.print the first letter of every word:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.println("What is your name?");
    String name = sc.nextLine();
    sc.close();

        int length = name.length();
        char first_initial = name.charAt(0);

        System.out.print("Your initials are: ");
        System.out.print(first_initial);
        for (int i = 1; i <= length - 1; i++) {
            if (name.charAt(i) == ' ') {
                System.out.print(" " + name.charAt(i+1));
            }
        }
}

The program works fine but I want to expand the program further so that I can store the initials in an array/string for use further down the line.

Thank you!

1 answer

  • answered 2017-11-12 20:05 Akshay jain

    you can use the

    String.valueOf(char ) method of the String Class
    

    to convert your char initials to string and then you can use it further.