Reputation: 15
I have a slight problem with displaying the vowels in a word that I input. I have vowels = AEIOU. I was thinking maybe that was the problem, but I'm not sure and I don't know how to fix it.
Here's what it gives me:
Enter a word: ANTARTICA
Here are the vowels in ANTARTICA: AEIOU
Vowel count: 4
Process completed.
So as you can see, it counted the vowels, I just want them to be displayed also. How would I do that? (I'm a noob coder....)
import static java.lang.System.*;
import java.util.*;
public class Java2106{
public static void main(String[] args) {
new Solution();
}}
class Solution
{
String word;
int count;
String vowels;
Solution()
{
input();
output();
}
public void input()
{
Scanner scan = new Scanner(in);
out.print("Enter a word: ");
word = scan.nextLine();
}
public void output()
{
vowels = "AEIOU";
count = 0;
out.println();
out.print("Here are the vowels in " + word + ": " + vowels);
for (int x = 0; x < word.length(); x++)
{
String let = word.substring(x,x+1);
if (vowels.contains(let))
count++;
}
out.println("\nVowel count: " + count);
}
}
Upvotes: 0
Views: 155
Reputation: 128
There are loads of ways to do this.
You could have an array for the vowels like this:
char[] vowels = {'a','e','i','o','u'};
Then do a simple for loop that goes to through every letter of your string word:
int count = 0;
String vowelsInWord = "";
for(int i=0; i<word.length; i++){
for(int j=0; j<vowels.length; j++){
if(word.charAt(i).toLowerCase() == vowels[j]){
vowelsInWord += word.charAt(i).toLowerCase();
count++;
}
}
}
System.out.println("Vowels in word: " + vowelsInWord);
System.out.println("Vowel Count: " + count);
Upvotes: 0
Reputation: 4187
When you compare the letters you have to lowercase or ignore case both the letters. I have added one more method java8()
which shows you how to solve this using Java 8 lambda functions.
I would highly recommend you to read basic Java for your own benefit and learn how to write a Java class as the below is not a good implementation for a class. Your logic is way too complex for a simple program like this so I would highly recommend you to go back to basics.
import static java.lang.System.in;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Java2106 {
public static void main(String[] args) {
new Solution();
}
}
class Solution {
String word;
int count;
String vowels;
Solution() {
input();
output();
java8();
}
private void java8() {
Scanner scan = new Scanner(in);
out.print("Enter a word: ");
word = scan.nextLine();
List<Character> letters = convert(word.toLowerCase());
letters.removeIf(c -> (c.charValue() != 'a' && c.charValue() != 'e' && c.charValue() != 'i' && c.charValue() != 'o' && c.charValue() != 'u'));
System.out.println("Vowels in given word: " + letters.size());
scan.close();
}
private List<Character> convert(String word) {
List<Character> letters = new ArrayList<Character>();
for (Character c : word.toCharArray()) {
letters.add(c);
}
return letters;
}
public void input() {
Scanner scan = new Scanner(in);
out.print("Enter a word: ");
word = scan.nextLine();
}
public void output() {
vowels = "AEIOU";
count = 0;
out.println();
out.print("Here are the vowels in " + word + ": " + vowels);
for (int x = 0; x < word.length(); x++) {
String let = word.substring(x, x + 1);
if (vowels.toLowerCase().contains(let.toLowerCase()))
count++;
}
out.println("\nVowel count: " + count);
}
}
One of the simple implementation:
import java.util.Scanner;
public class Java2106 {
public static void main(String[] args) {
int count = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = scan.nextLine();
for (char c : word.toLowerCase().toCharArray()) {
if (isVowel(c)) {
count++;
}
}
System.out.println("Total Vowels : " + count);
scan.close();
}
private static boolean isVowel(char c) {
return (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
}
}
Upvotes: 1