-1

Design and implement an application that reads a string from the user, then determines and prints the number of vowels and consonants which appear in the string. Use a switch statement inside a loop.

A typical program output might be:

Enter a sentence
> My dog has fleas!
Sentence is : My dog has fleas!
VowelVount is : 4
ConsonantCount is : 9

My code is:

import java.util.Scanner;

public class VnC{

  public static void main(String [] args){
    String text;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a sentence");
    text = scan.nextLine();
    System.out.println("Sentence is : " + text);
    text = text.toLowerCase();
    switch(text) {
      case 'a':
      case 'e':
      case 'i':
      case 'o':
      case 'u':
        vowelCount++;
        System.out.println("VowelCount : " + vowelCount);
        break;
      default:
        consonanyCount++;
        System.out.println("ConsonantCount is : " + consonantCount);
        break;
    }
  }

}
Mat
  • 202,337
  • 40
  • 393
  • 406
user1610594
  • 13
  • 1
  • 3
  • Your problem is that you don't loop throug the string. A simple google search for `java string for loop` will help you – Cole Tobin Aug 20 '12 at 00:03

3 Answers3

1

You're on the right path, and almost there. You need to loop through all the characters in your input string (text). Use a for loop for this, and switch on each character as opposed to the entire string.

K Mehta
  • 10,323
  • 4
  • 46
  • 76
  • How would I go about doing that? – user1610594 Aug 20 '12 at 00:06
  • I understand that this is homework, so I wouldn't want to give you the answer. You can try searching for "iterate string in java" in your favorite search engine, and that should give you all the information you need. – K Mehta Aug 20 '12 at 00:07
  • This isn't homework. Its just a hobbie of mine. – user1610594 Aug 20 '12 at 00:08
  • 1
    Either way, you're trying to learn :) And the best way to learn is to go out and research. You'll find what you're looking for, and in the process, you'll learn other things that'll help you in the future. – K Mehta Aug 20 '12 at 00:09
  • Would I need to put this code injust after the text = text.toLowerCase() code? for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') { count++; – user1610594 Aug 20 '12 at 00:10
  • that works :) also note that if `c` is not one of 'a', 'e', 'i', 'o', or 'u', that does NOT mean it's a constant. You'll have to avoid spaces, exclamation marks, periods, etc. – K Mehta Aug 20 '12 at 00:11
0

Check out this. Might help:

import java.util.Scanner;

public class XX{

  public static void main(String [] args){
    String text;
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter a sentence");
    text = scan.nextLine();
    System.out.println("Sentence is : " + text);
    text = text.toLowerCase();
    int vowelCount = 0 ;
    int consonantCount = 0 ;
    text = text.replaceAll("[-+.^:, !]",""); // remove chars that you don't want to count

    for(int i = 0; i < text.length() ;i++ ){
        if(text.charAt(i)== 'a' ||text.charAt(i)== 'e' ||text.charAt(i)== 'i' ||text.charAt(i)== 'o' || text.charAt(i)== 'u')
            vowelCount++;
            else
            consonantCount++;
    }

    System.out.println("VowelCount : " + vowelCount);
    System.out.println("ConsonantCount is : " + consonantCount);
    }

    }
Arpssss
  • 3,850
  • 6
  • 36
  • 80
-1
for (char ch : text.toCharArray()) { 
  switch(ch) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
      vowelCount++;
      break;
    default:
      consonanyCount++;
      break;
  }
} 
System.out.println("VowelCount : " + vowelCount);
System.out.println("ConsonantCount is : " + consonantCount);
mostar
  • 4,723
  • 2
  • 28
  • 45
  • 5
    Please avoid spoon feeding the answer to new programmers. This does not help them learn at all. – K Mehta Aug 20 '12 at 00:08
  • 1
    this was a very trivial one so i just wrote the answer. i hope he will try to understand what is different with this code. and i think going through sample codes is always a good practice. – mostar Aug 20 '12 at 00:13
  • 3
    What's trivial for you may not be trivial for a new learner. You should explain your sample. Or you should try to give a different sample that covers the same concepts that the OP is trying to learn more about. If this was a school assignment, just handing over the solution would only screw the OP up in the long run. – K Mehta Aug 20 '12 at 00:15
  • 3
    Please note: you/we should apply this principle *whether or not* this is actual "homework" set by a teacher or instructor. – Stephen C Aug 20 '12 at 00:44