【问题标题】:Changing a string to a Char array将字符串更改为 Char 数组
【发布时间】:2013-11-23 22:52:57
【问题描述】:

一旦一个值被存储为一个字符串,你如何循环字符串并将每个值分配给一个字符数组?还必须计算数组中每个元音的出现次数。

这是我当前的代码:

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

  }

}

【问题讨论】:

  • 字符串基本上是一个带有一些额外细节的字符数组。但是,是的......它基本上已经是一个 char 数组了。
  • 感谢您的回复!问题的最初部分是:“循环遍历字符串并将每个值分配给一个 char 数组”。我想知道如何在我的代码中执行此操作,还是已经完成了?
  • 使用你的编码方式(虽然 Nick G 的方式肯定更好,但对你来说可能有点太高级了),删除 char[] letter = userName.toCharArray(); 并将 if (userName.charAt(0) == 'a') { 替换为 if (userName.charAt(loop) == 'a') {正如 pobrelkey 所建议的那样。现在您必须对剩余的元音执行相同的操作(if 条件并以loop 作为索引进行计数)。请注意,此当前实现仅捕获a,而不是A。你可以通过写if (userName.charAt(loop) == 'a' || userName.charAt(loop) == 'A') { 来解决这个问题。同样,这种方式并不漂亮。 :)
  • 在现实世界中,您不需要将username 中的值复制到数组中来计算元音,而只需使用username.charAt() 来单独获取字符值。即使出于某种原因您确实想从String 创建一个char 数组,您也只需调用yourString.toCharArray()。另一方面,如果你的教授让你编写不必要的代码只是为了告诉你如何实例化一个数组并处理它的元素,那是另一回事......
  • 我现在得到了我的解决方案,非常感谢大家!如果你不介意我问,有没有关于堆栈溢出的部分,我可以为像我这样的新手写我熟悉的内容的答案?我想为我知道的问题提供答案,而不是总是接受它们!大声笑

标签: java string for-loop char


【解决方案1】:
String str = "stackoveflow";
char[] aa= str.toCharArray();

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

str.charAt(i);

【讨论】:

    【解决方案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 是微不足道的 - 例如结果 == 空? 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}
    

    【讨论】:

      【解决方案3】:

      使用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);
          }
      }
      

      【讨论】:

        【解决方案4】:
        if (userName.charAt(loop) == 'a') {
        

        【讨论】:

          猜你喜欢
          • 2018-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-08-04
          • 2021-08-26
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多