Skip to content

Instantly share code, notes, and snippets.

@andymuncey
Created July 25, 2016 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymuncey/f4137bb2ea7b41a1bd0092f3512f0570 to your computer and use it in GitHub Desktop.
Save andymuncey/f4137bb2ea7b41a1bd0092f3512f0570 to your computer and use it in GitHub Desktop.
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