My instructor isn't too strict on the manner in which we complete this as we are fairly new to programming. I tampered with my code and found a solution. I understand it may not be the best solution, but for the most part, as long as proper punctuation is used within the String, then the word count as well as the vowel count do go up as desired.
Most of the answers I received were a little advanced for me. We haven't covered a lot of these concepts, but thank you guys so much. Someone pointed out that I was using ".equals" to compare a single character instead of using the "==". This solution helped me the most!!
If anyone can add on to my solution to allow ANY user input be converted into the correct word count and vowel count then thank you!!
ex. 1 (States the correct word count and vowel count):
String: "Let us go over there!"
vowels: 7
words: 5
ex. 2 (States the incorrect word count and vowel count):
These are the values when no punctuation is used
String: "Let us go over there"
vowels: 7
words: 4
import java.util.*;
class StringCount {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
String user;
int s, charCount, wordCount, vowelCount;
char[] vowel = {'a', 'e', 'i', 'o', 'u',};
char[] punct = {' ', '.', ';','?', '!', '-'};
s = 0;
wordCount = 0;
vowelCount = 0;
System.out.println("Please enter any string!");
user = input.nextLine();
user = user.toLowerCase();
charCount = user.length();
for (int i = 0; i < charCount; i++) {
if(user.charAt(i) == vowel[0] ||
user.charAt(i) == vowel[1] ||
user.charAt(i) == vowel[2] ||
user.charAt(i) == vowel[3] ||
user.charAt(i) == vowel[4])
vowelCount++;
if(user.charAt(i) == punct[0] ||
user.charAt(i) == punct[1] ||
user.charAt(i) == punct[2] ||
user.charAt(i) == punct[3] ||
user.charAt(i) == punct[4] ||
user.charAt(i) == punct[5])
wordCount++;
}
System.out.println("Vowels: " + vowelCount);
System.out.println("Words: " + wordCount);
}
}