How do you use charAt with an array?

2019-09-02 15:46发布

问题:

i am having a bit of trouble in implementing charAt with an array. I am a beginner in Java (and this is my only coding experience i suppose).

The objective: To create a program that the user inputs any string, and the total number of vowels are recorded in the output (case sensitive)

example: Input: charActer Output: a = 1 A = 1 e = 1

import java.util.Scanner;
public class HW5 {
public static void main(String[] args) {
String [] alphabets = 
{"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"};
String vowels = "aAeEiIoOuU";
int found = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter any word: ");
String inputStr = sc.nextLine();
for(int i=0;i<alphabets.length;i++)
    {
        if(alphabets.charAt[i] == vowels)

*Note: Program is not complete.

回答1:

You need to check each character of inputStr (dunno what alphabets is about in your code) and see if it can be found in the vowels string.

    String vowels = "aAeEiIoOuU";
    int found = 0;
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter any word: ");
    String inputStr = sc.nextLine();
    for (int i = 0; i < inputStr.length(); i++) {
        if (vowels.indexOf(inputStr.charAt(i)) >= 0) {
            found += 1;
        }
    }


回答2:

The documentation is helpful if you're having trouble understanding a method or class.

Having said that, there are lots of ways to count vowels in a String.

Your output indicates that you need the counts per vowel per case, and not just the count of all vowels. To do this you will need a map in order to keep track.

Consider something like

String input = "A string with vowels in it";
Map<Character, Integer> counts = new HashMap<≥();
for (int i = 0; i < input.length; i++) {
    char c = input.chart(i);
    if (c == 'a') {
        int tmp = counts.getOrDefault('a', 0);
        tmp++;
        counts.put('a', tmp);
    } else if (c == 'A') {
        // same logic as above for uppercase A
    } // other else if statements for e, E, i, I, o, O, u, U
}

// the map has all counts per vowel / case

After the map has all counts you can iterate its entry set to print the output you need

for (Map.Entry<Character, Integer> e : counts. entrySet()) {
    System.out.println(e.getKey() + " = " + e.getValue());
}

If you only need the number of values without breaking it down into which vowels, consider something like (not tested)

String vowels = "AaEeIiOoUu";
String input = "Hello World!";
int numVowels = 0;
for (int i = 0; i < input.length; i++) {
    char c = input.charAt(i);
    if (vowels.indexOf(c) >= 0) {
        numVowels++;
    }
}

// do something with numVowels

--

Break the problem into simple steps

  1. Define the vowels to look for
  2. Initialize your counter variable (numVowels)
  3. Loop through the input string and check each character against the ones defined in 1 (vowels).
  4. For each vowel you find, increment your counter variable.


回答3:

public class Vowels {

public static void main(String[] args) {
    Map<Character, Integer> vowels = new HashMap<>();
    Scanner sc = new Scanner(System.in);
    System.out.print("Please enter any word: "); //"charActer";
    String str = sc.nextLine();
    for (int i = 0; i < str.length(); i++) {
        Character c = str.charAt(i);
        if (c == 'a'
                || c == 'A'
                || c == 'e'
                || c == 'E'
                || c == 'i'
                || c == 'I'
                || c == 'o'
                || c == 'O'
                || c == 'u'
                || c == 'U') {
            if (vowels.containsKey(c)) {
                vowels.put(c, vowels.get(c) + 1);
            } else {
                vowels.put(c, 1);
            }
        }
    }

    for (Map.Entry<Character, Integer> entry : vowels.entrySet()) {
        System.out.print(entry.getKey() + "=" + entry.getValue() + " ");
    }
}}
Input : charActer 
Output : a=1 A=1 e=1