I need help writing this in Java code. Write a static method that takes a string
ID: 3834805 • Letter: I
Question
I need help writing this in Java code.
Write a static method that takes a string argument containing a person's full name as a parameter, and returns a string containing just their initials. The name may have many parts or just one, e.g., for the name "Cher" the method returns "C", for the name "Edna del Humboldt von der Schooch" the method returns "EdHvdS". Test your code.
Write a static method to find the index of the first vowel in a string (returning -1 if there are no vowels). Note an easy way to write the boolean condition "character ch is a vowel" is
Explanation / Answer
StringInitials.java :
________________
import java.util.Scanner;
public class StringInitials {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter persons full name:");
String name = sc.nextLine();
String initial = initials_Finding(name);
System.out.println("Initials:"+initial);
System.out.println("Enter String to find first vowel index in it:");
String str = sc.nextLine();
int index = index_Vowel(str);
System.out.println("Index of first vowel in "+str+" is : "+index);
}
public static String initials_Finding(String name){
String initial="";
String a[] = name.split(" ");
for(int i=0;i<a.length;i++)
initial = initial.concat(Character.toString(a[i].charAt(0)));
return initial;
}
public static int index_Vowel(String str){
int index;
for(int i=0;i<str.length();i++){
char ch = str.charAt(i);
if("aeiouAEIOU".indexOf(ch) >= 0){
index = i;
return index;
}
}
return -1;
}
}
Sample Input and Output:
______________________
Enter persons full name:
Edna del Humboldt von der Schooch
Initials:EdHvdS
Enter String to find first vowel index in it:
Chegg CSE Experts
Index of first vowel in Chegg CSE Experts is : 2