Write a program that reads a word and prints the number of vowels in the word. F
ID: 3587066 • Letter: W
Question
Write a program that reads a word and prints the number of vowels in the word. For this exercise, assume that a e i o u y are vowels. For example, if the user provides the input "Harry", the program prints 2 vowels.
import java.util.Scanner;
/**
*
* @author oneken
*/
public class HW2Q3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enster a word: ");
String strLine = in.next();
int count = 0;
for (int x = 0; x < str.length(); x++) {
char ch = str.charAt(x);
//check for characters a,e,i,o,u,y
if ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y'));
{
count++;
}
}
System.out.println("Number of vowels: " +count);
}
}
keep getting error - not sure what I am doing wrong.
Explanation / Answer
//There are two errors you are taking input in string named strLine but usinf str.length() , you must use same name //at both place , for your code i changed String str which was String strline also in if condition checking there was //comma which violated condition checking. Correct code is provided below.
import java.util.Scanner;
/**
*
* @author oneken
*/
public class HW2Q3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please enster a word: ");
String str = in.next();
int count = 0;
for (int x = 0; x < str.length(); x++) {
char ch = str.charAt(x);
//check for characters a,e,i,o,u,y
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y')
{
count++;
}
}
System.out.println("Number of vowels: " +count);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.