Daniel Andrews Daniel Andrews - 5 months ago 9
Java Question

I am trying to find vowels then add them, but did i do this right?

Idk if I did this right.. I just want to know how to add the vowels together to make three because my name is Daniel 3 total vowels but its not working when I add other names.

import java.util.Scanner;


public class Pratclass {

public static void main(String[] args)
{
Scanner std= new Scanner(System.in);
String string1;
System.out.println("What is your name");
string1= std.nextLine();

int count= 0;
int vowels=0;

for(String retval: string1.split(""))
{
for(int i=0; i < retval.length(); i++)
{
char c= retval.charAt(i);
if(c== 'a' || c== 'e' || c== 'i' || c== 'o'|| c== 'u')
{
vowels++;
}
else
{
count++;
}
System.out.println(retval.substring(0,1) + retval.substring(1) + vowels);
vowels=0;
}
}
int total=vowels++;

total=(total + vowels++ + vowels); < How can I count the vowels
System.out.println();

System.out.println(total);

}
}

Answer

also you can use this...

import java.util.Scanner;

public class Pratclass1 {

public static void main(String[] args) {
Scanner std= new Scanner(System.in);
String string1;
System.out.print("What is your name :");
string1= std.nextLine();

int count= 0;
int vowels=0;

char[] ar = new char[string1.length()];

for(int i = 0; i < ar.length; i++) {
    ar[i] = string1.charAt(i);
    if(ar[i] == 'a' || ar[i] == 'e' || ar[i] == 'i' || ar[i] == 'o'|| ar[i] == 'u') {
        vowels++;
    } else {
        count++;
    }
}

int total=vowels;
System.out.println();
System.out.println(total);
}   

}