I have a serious problem here!
i understand that the switch is wrong(the Str class should be fine).
I wonder what datatype should it be? and if "char" how i am going to key in int value and how to use scanner on it ?
The display menu is fix. mean 1,2,3,4,e
Fast reply is needed ! ><
import java.util.Scanner;
public class testString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String choice;
int i,total ;
System.out.print("Enter a string: ");
String message = input.nextLine();
Str myString = new Str(message);
int vowel = myString.countVowels(message);
int consonant = myString.countConsonants(message);
do {
System.out.println("1. Count the number of vowels in the string.");
System.out.println("2. Count the number of consonants in the string.");
System.out.println("3. Count both the vowels and consonants in the string.");
System.out.println("4. Enter another String.");
System.out.println("e. Exit the program.");
System.out.println("Choice = ");
choice = input.next();
if (choice == "e") {
System.exit(0);
}
i = Integer.parseInt(choice);
switch (i) {
case 1 : if (vowel == 1 | vowel == 0) {
System.out.println("There is "+vowel+" vowel.");
} else
System.out.println("There are "+vowel+" vowels.");
break;
case 2 :if (consonant == 1 | consonant == 0) {
System.out.println("There is "+consonant+ "consonant.");
} else
System.out.println("There are "+consonant+ " consonants.");
break;
case 3 : total = consonant + vowel;
break;
case 4 :System.out.println("Enter a string: ");
message = input.nextLine();
myString = new Str(message);
break;
}
} while (choice != "e");
}
}
import java.util.Scanner;
public class Str {
static Scanner input = new Scanner(System.in);
String Message;
Str() {
}
Str(String newMessage) {
Message = newMessage;
}
int countVowels(String message) {
int count = 0;
String lowerMessage = message.toLowerCase();
for (int i = 0 ; i < lowerMessage.length() ; i++) {
char x = lowerMessage.charAt(i);
if (x =='a' || x =='e' || x =='i' || x == 'o' || x == 'u') {
count++;
}
}
return count;
}
int countConsonants(String message) {
int count = 0;
String lowerMessage = message.toLowerCase();
for(int i = 0 ; i < lowerMessage.length() ; i++) {
char x = lowerMessage.charAt(i);
if ( x == 'q' || x == 'e' || x == 'r' || x == 't' || x == 'y' || x == 'p' || x =='s' || x == 'd'
|| x == 'f' || x == 'g' || x =='h' || x == 'j' || x =='k' || x == 'l' || x == 'z'
|| x == 'x' || x == 'c' || x == 'v' || x == 'b' || x == 'n' || x == 'm') {
count++;
}
}
return count;
}
}