Once a value is stored as a String, how can you loop through string and assign each value to a char array? The occurrences of every vowel in array must also be counted.
This is my current code:
public class Part1_5 {
/**
* Method that gets user name and stores it as a string. Each value then
* assign to a char array. no of vowels are counted and no of each printed
*/
public static void main(String[] args) {
// Setting up scanner
Scanner scanner = new Scanner(System.in);
// declaring string for name
String userName = null;
// declaring ints to hold total no of each vowel
int totalOfA = 0;
int totalOfE = 0;
int totalofI = 0;
int totalofO = 0;
int totalofU = 0;
// Get user input for name
System.out.println("Please enter your Name...");
userName = scanner.nextLine();
for (int loop = 0; loop < userName.length(); loop++) {
// declaring char array
char[] letter = userName.toCharArray();
if (userName.charAt(0) == 'a') {
totalOfA++;
}
}
System.out.println(totalOfA);
}
}
String str = "stackoveflow";
char[] aa= str.toCharArray();
And to directly get a char from string, you can use:
str.charAt(i);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With