0

I am currently stuck on the first loop of the code. I'm having a difficult time comparing the character at an index within the string to the first vowel a (Keeps saying that char cannot be dereferenced...).

I would just like to know how to properly compare any letter within the string to any vowels (a, e, i, o, u). I would also like to know if I can compare a certain letter within the string to the entire array, or would I just have to compare to every individual vowel??

import java.util.*;

class StringCount {

   public static void main(String [] args) {

      Scanner input = new Scanner(System.in);
      String user;
      int charCount;
      char[] vowel = {'a', 'e', 'i', 'o', 'u'};
      System.out.println("Please enter any string!");
      user = input.nextLine();
      charCount = user.length();

      for (int i = 0; i < charCount; i++) {
         if(user.charAt(i).equals('a') {

         }
      }

   }

}
Kaustubh Khare
  • 3,280
  • 2
  • 32
  • 48

6 Answers6

0

You are missing a couple of things here: a. Character can be in uppercase or lowercase. b. Word seperators, they may be comma , space, etc.

 import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    public class WaitTest {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
              String user;
              int charCount;
              List<Character> vowels = new ArrayList();
              //add all vowels to list inclusing uppercase 
 a,e,i,o,u,A,E,I,O,U
              vowels.add('a');
              vowels.add('e');

              List<Character> wordSeperators = new ArrayList();
              //add your word seperators here
              wordSeperators.add(' ');
              wordSeperators.add(',');

              System.out.println("Please enter any string!");
              user = input.nextLine();
              charCount = user.length();
               int vowelCount=0,wordCount=0;
              for (int i = 0; i < charCount; i++) {
                 char c= user.charAt(i);
                 if(vowels.contains(c)) {
                     vowelCount++;
                 }else if (wordSeperators.contains(c)){
                     wordCount++;
                 }
              }
            }
    }
akshaya pandey
  • 997
  • 6
  • 16
0

If you just want to check your user enter a word with a char in your list you could use Java.lang.String.contains() Method with your char list in the loop

If u want to check your user enter is exctely a char in your list, you could use Java.lang.String.equal() Method with in a loop

Yiao SUN
  • 908
  • 1
  • 8
  • 26
0

You will need to modify your code to compare every input char with your vowel array.

for (int i = 0; i < charCount; i++) {
  for (char v : vowel) {
    if(user.charAt(i) == v {

      //here is what you do if a vowel <v> is found

    }
  }
}

Hope this helps.

Jan Kl.
  • 69
  • 2
  • 13
  • hmm.. I completely understand your if statement so thanks for that! I'm just starting out in college so excuse my knowledge thus far. What exactly is the second for loop saying exactly?? I don't understand the colon – alexander sanchez Nov 07 '17 at 09:45
  • How to use '.equals()' method with primitive type like user.charAt(i).equals(v) ?? – Priyantha Nov 07 '17 at 10:02
  • The second loop is going through every element in your vowel array. (Eg. first time the "v" equals 'a', second time 'e' and so on....) – Jan Kl. Nov 07 '17 at 10:41
  • 1
    @Dev4World you are right, my fault. Chars should be compared by " == ", not by equals function. Thanks and sorry for that. Already edited. – Jan Kl. Nov 07 '17 at 10:42
0

Another possible solution:

    if (Arrays.binarySearch(vowel, user.charAt(i)) >= 0)  {

    }
Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
0

Update:

equals() is used for Objects (String, Integer, etc...)

For primitives like int, boolean, char etc, you have to use == Try with this.

 public static void main(String[] args) {

 int count =0;
    String user;

    Scanner input = new Scanner(System.in);

    System.out.println("Please enter any string!");
    user = input.nextLine();
    System.out.println(user);
    int inputLength = user.length();

      try{
          for (int i = 0; i < inputLength; i++) {

              char letter = user.charAt(i);

     if(letter=='a'||letter=='e'||letter=='i'||letter=='o'||letter=='u') {

                  count +=1;  
             }

          }

      }catch (Exception e) {
        // TODO: handle exception
    }
      System.out.println("FINAL COUNT "+count);

}
Priyantha
  • 4,839
  • 6
  • 26
  • 46
  • Oh, so my code isn't read properly because of the ".equals?" That makes sense! I just went and tampered around with my code and this was my main problem. Thanks bro! I didn't realize that. I was recently comparing strings, so I guess I kept trying to use the ".equals" for the char type. So in essence, Strings and the Char type are completely different right? – alexander sanchez Nov 07 '17 at 10:33
0

My instructor isn't too strict on the manner in which we complete this as we are fairly new to programming. I tampered with my code and found a solution. I understand it may not be the best solution, but for the most part, as long as proper punctuation is used within the String, then the word count as well as the vowel count do go up as desired. Most of the answers I received were a little advanced for me. We haven't covered a lot of these concepts, but thank you guys so much. Someone pointed out that I was using ".equals" to compare a single character instead of using the "==". This solution helped me the most!!

If anyone can add on to my solution to allow ANY user input be converted into the correct word count and vowel count then thank you!!

ex. 1 (States the correct word count and vowel count): String: "Let us go over there!" vowels: 7 words: 5

ex. 2 (States the incorrect word count and vowel count): These are the values when no punctuation is used String: "Let us go over there" vowels: 7 words: 4

import java.util.*;

class StringCount {

public static void main(String [] args) {

  Scanner input = new Scanner(System.in);
  String user;
  int s, charCount, wordCount, vowelCount;
  char[] vowel = {'a', 'e', 'i', 'o', 'u',};
  char[] punct = {' ', '.', ';','?', '!', '-'};
  s = 0;
  wordCount = 0;
  vowelCount = 0;

  System.out.println("Please enter any string!");
  user = input.nextLine();
  user = user.toLowerCase();
  charCount = user.length();
  for (int i = 0; i < charCount; i++) {
     if(user.charAt(i) == vowel[0] ||
        user.charAt(i) == vowel[1] ||
        user.charAt(i) == vowel[2] ||
        user.charAt(i) == vowel[3] ||
        user.charAt(i) == vowel[4])
        vowelCount++;

     if(user.charAt(i) == punct[0] ||
        user.charAt(i) == punct[1] ||
        user.charAt(i) == punct[2] ||
        user.charAt(i) == punct[3] ||
        user.charAt(i) == punct[4] ||
        user.charAt(i) == punct[5])
        wordCount++;
  }
  System.out.println("Vowels: " + vowelCount);
  System.out.println("Words: " + wordCount);

  }

  }
Priyantha
  • 4,839
  • 6
  • 26
  • 46