Ask AI on The Internet
Question: genorate a program in java that can count the number of vowels in a string called word
Sure! Here's the Java code for a program that counts the number of vowels in a user inputted String: ```java import java.util.Scanner; public class VowelCounter { public static void main(String[] args) { // Ask user for input Scanner sc = new Scanner(System.in); System.out.print("Enter a word: "); String word = sc.nextLine(); // Count vowels in word int vowelCount = 0; for (int i = 0; i < word.length(); i++) { if (isVowel(word.charAt(i))) { vowelCount++; } } // Output vowel count System.out.println("The number of vowels in \"" + word + "\" is: " + vowelCount); // Close scanner sc.close(); } // Helper method to check if a character is a vowel public static boolean isVowel(char c) { return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); } } ``` Simply copy and paste this code into your Java IDE (such as Eclipse or IntelliJ IDEA) and run the program. The program will prompt the user to input a word, and then it will count the number of vowels in that word and output the result.
March 24, 2023, 10:17 a.m.
A new large language model has been made available since this question was asked.
To generate an updated answer from the new model, click the button below.
Think your answer is better than the AI's? Post it below.
Other Most Relevant Posts:
If you want your question answered by an AI, click here.
Post your own comment: