简体   繁体   中英

I need to create a program that allows a user to input any string. The program must determine how many vowels and words are within the string

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') {

         }
      }

   }

}

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

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

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.

Another possible solution:

    if (Arrays.binarySearch(vowel, user.charAt(i)) >= 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);

}

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

  }

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM