-5

I just got into my 2nd programming subject but my prof just throws stuff at us that he didn't teach so I'm guessing that I need help from people because searching the internet's help ain't enough

So the output that I'm looking for is like this

Enter word : #$nsaei!
result = 2
  • 1
    Have a look at the answer [here](http://stackoverflow.com/questions/15562265/checking-for-alphabets-in-a-string-in-java). It should be a simple adjustment to get what you're looking for – Dana Dec 22 '16 at 03:14
  • http://stackoverflow.com/help/on-topic – AJ. Dec 22 '16 at 03:18
  • Please edit your question so as to give more details about your situation and the problems you are facing. – Right leg Dec 22 '16 at 04:02

2 Answers2

0

Try something like this using a Scanner, converting the String input into a char array using toCharArray() and checking each char using isLetterOrDigit and checking if the character is not in "AEIOUaeiou" using indexOf() and therefore not a vowel:

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter a word:");
    String input = sc.nextLine();

    int consonantOrDigits = 0;
    for(char c : input.toCharArray()) {
      if(Character.isLetterOrDigit(c) && "AEIOUaeiou".indexOf(c) == -1)
        consonantOrDigits++;
    }

    System.out.println(consonantOrDigits);
  }
}

Try it here!

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40
  • Wow thanks man I'm gonna study this later But what if we don't count the vowels as well(because this might help me in the future) – Reinbow Aisu Dec 22 '16 at 03:31
0

You can do it this way:

System.out.println("Enter word: ");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();

int count = 0;

for (int i = 0; i < input.length(); i++) {
    if (String.valueOf(input.charAt(i)).matches("^[a-zA-Z0-9&&[^aeuio]]*$")) {
        count++;
    }
}

System.out.println("result: " + count);

Hope this helps.

Nurjan
  • 5,889
  • 5
  • 34
  • 54
  • `Enter word: #$nsaei! result: 5` – Scary Wombat Dec 22 '16 at 04:23
  • @ScaryWombat It seems that the question was edited. The way I understood the question was to count everything except for the special symbols. But it turns out that vowels have to be considered as well. I will edit the answer. Thanks for the downvote. – Nurjan Dec 22 '16 at 04:30
  • let me know when you have edited and I can remove the downvote. (I know this is not your fault, but as it stands it is an incorrect answer). – Scary Wombat Dec 22 '16 at 04:32
  • @ScaryWombat I edited the answer. It should work now. – Nurjan Dec 22 '16 at 04:45