Skip to content

Instantly share code, notes, and snippets.

@andymuncey

andymuncey/Main.java Secret

Created Jul 25, 2016
Embed
What would you like to do?
Java code that requires refactoring
package uk.ac.chester;
public class Main {
public static void main(String[] args) {
System.out.println("Please type a phrase");
java.util.Scanner scanner = new java.util.Scanner(System.in);
String text = scanner.nextLine();
text = text.toLowerCase();
int countA = 0;
int countE = 0;
int countI = 0;
int countO = 0;
int countU = 0;
for (int i = 0; i<text.length(); i++){
char letter = text.charAt(i);
switch (letter){
case 'a':
countA++;
break;
case 'e':
countE++;
break;
case 'i':
countI++;
break;
case 'o':
countO++;
break;
case 'u':
countI++;
break;
default:
break;
}
}
char mostCommonVowel = 'a';
int highCount = countA;
if (countE > countA){
mostCommonVowel = 'e';
highCount = countE;
}
if (countI > highCount){
mostCommonVowel = 'i';
highCount = countI;
}
if (countO > highCount){
mostCommonVowel = 'o';
highCount = countO;
}
if (countU > highCount){
mostCommonVowel = 'u';
highCount = countU;
}
System.out.println("The most common vowel is: " + mostCommonVowel);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment