Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding Index of same element multiple times, Java

I have my code here, I enter a full name with three words, and I need to get the initials of the first two words, Like:

Input = Anthony Edward Stark

Output = A.E.Stark

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Pls help me

>Solution :

I think this would do the job

public static void main(String[] args) {
        String input = "Anthony Edward Stark";
        String names[] = input.split(" ");
        String result = "";
        for(int i = 0; i<names.length; i++){
            if(i< names.length-1){
                result += names[i].charAt(0) +".";
            }else{
                result += names[i];
            }
        }
        System.out.println(result);
    }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading