将字符串更改为Char数组

时间:2013-11-11 23:40:17

标签: java string for-loop char

一旦将值存储为String,您如何循环遍历字符串并将每个值分配给char数组?必须计算阵列中每个元音的出现次数。

这是我目前的代码:

public class Part1_5 {

  /**
   * Method that gets user name and stores it as a string. Each value then
   * assign to a char array. no of vowels are counted and no of each printed
   */
  public static void main(String[] args) {

    // Setting up scanner
    Scanner scanner = new Scanner(System.in);

    // declaring string for name
    String userName = null;

    // declaring ints to hold total no of each vowel
    int totalOfA = 0;
    int totalOfE = 0;
    int totalofI = 0;
    int totalofO = 0;
    int totalofU = 0;

    // Get user input for name
    System.out.println("Please enter your Name...");
    userName = scanner.nextLine();

    for (int loop = 0; loop < userName.length(); loop++) {

      // declaring char array
      char[] letter = userName.toCharArray();

      if (userName.charAt(0) == 'a') {

        totalOfA++;

      }

    }

    System.out.println(totalOfA);

  }

}

4 个答案:

答案 0 :(得分:2)

String str = "stackoveflow";
char[] aa= str.toCharArray();

要直接从字符串中获取字符,您可以使用:

str.charAt(i);

答案 1 :(得分:2)

如何迭代(并计算)字符串中的所有字符?

元音的数量是否必须区分大小写?

Map<Character, Integer> count = new HashMap<Character, Integer>();
char[] chars = "this is a test".toCharArray();
for (char curr : chars){
    Integer tmp = count.get(curr);
    if (tmp == null){ tmp = new Integer(0); }
    tmp++;
    count.put(curr, tmp);    
}
System.out.println(count.get("a".charAt(0)));
System.out.println(count.get("e".charAt(0)));
System.out.println(count.get("i".charAt(0)));
System.out.println(count.get("o".charAt(0)));
System.out.println(count.get("u".charAt(0)));

哪个给你......

1
1
2
null
null

处理null是微不足道的 - 例如结果== null? 0:结果

编辑:提高100%不区分大小写!!

Map<Character, Integer> count = new HashMap<Character, Integer>();
for (char curr :  "this IS a test".toLowerCase().toCharArray()){
    Integer tmp = count.get(curr);
    count.put(curr, tmp == null ? 1 : ++tmp);
}

同样的事情,但在Groovy ......

def count = "this IS a test".toLowerCase().collect().countBy{it}

答案 2 :(得分:0)

if (userName.charAt(loop) == 'a') {

答案 3 :(得分:0)

使用for loop中的计数变量指定userName String中的位置来计算元音。

此外,您甚至不需要char array来执行您正在执行的方法。但如果你确实需要一个,你应该在开始for loop之前定义它。为什么要创造这么多次?

您询问如何遍历String并将每个值分配给char array,但您不需要这样做:您只需执行您所做的操作,char[] letter = userName.toCharArray();

public class Part1_5 {
    public static void main(String[] args) {

        // Setting up scanner
        Scanner scanner = new Scanner(System.in);

        // declaring string for name
        String userName = null;

        // declaring ints to hold total no of each vowel
        int totalOfA = 0,totalOfE = 0,totalofI = 0,totalofO = 0,totalofU = 0;

        // Get user input for name
        System.out.println("Please enter your Name...");
        userName = scanner.nextLine();

        // declaring char array (declare it once, before the loop)
        char[] letter = userName.toCharArray();

        for (int loop = 0; loop < userName.length(); loop++) {

            // check and count for any vowel at every iteration of the loop
            if (userName.charAt(loop) == 'a')
                totalOfA++;
            else if (userName.charAt(loop) == 'e')
                totalOfE++;
            else if (userName.charAt(loop) == 'i')
                totalOfI++;
            else if (userName.charAt(loop) == 'o')
                totalOfO++;
            else if (userName.charAt(loop) == 'u')
                totalOfU++;
        }
        System.out.println(totalOfA);
    }
}