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;
	}
}

Recommended Answers

All 3 Replies

i change the switch to if...else method but it appear that whatever choice that i make it do nothing but looping the menu.

import java.util.Scanner;

public class testString	{

	public static void main(String[] args)	{

	Scanner input = new Scanner(System.in);

	String choice;
		
		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);
		while(message.length()!= 0) {
			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.nextLine();
			
			if (choice == "1")	{
				if (vowel == 1 | vowel == 0)	{
					System.out.println("There is "+vowel+" vowel.");
				} else
					System.out.println("There are "+vowel+" vowels.");
			}
			else if (choice == "2")	{
				if (consonant == 1 | consonant == 0)	{
					System.out.println("There is "+consonant+ "consonant.");
				} else
					System.out.println("There are "+consonant+ " consonants.");
			}
			else if (choice == "3")	{
				 System.out.println("There are a total of " +(consonant + vowel)+ "consonants and vowels");
			}
			else if (choice == "4"){
				System.out.println("Enter a string: ");
				message = input.nextLine();
				myString = new Str(message);
			}
			else if (choice == "e")	{
				System.exit(0);
			}
		} 
	}
}

use .equals when comparing Strings instead of ==

though I think it would have been more appropriate if the data type of choice is an int rather than a String

Thanks for reply
in the end i convert string to char to use the switch ==

choiceString = input.next();
		choice = choiceString.charAt(0);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.