【问题标题】:trouble with CharAtCharAt 的麻烦
【发布时间】:2013-12-04 21:45:57
【问题描述】:

我想制作一个简单的程序来接受用户输入,并在每个字母之间放置空格。例如,用户进入商场,它返回 M A L L(在同一行)。 我正在尝试在其中创建一个带有 if 语句的循环。但我认为我需要 CharAt,所以如果字符串的值大于 1,我会为字符串中的每个单个字符声明一个变量(即用户输入) .然后我会说在每个字母之间放置空格。我在AP计算机科学A,我们正在练习循环。这下的一切,都是我到目前为止所做的。方向在代码上面的注释中。我使用的是eclipse,java。

/**
 * Splits the string str into individual characters: Small becomes S m a l l
 */
public static String split(String str) {
    for (int i = 0; str.length() > i; i++) {
        if (str.length() > 0) {
            char space = str.charAt();
        }
    }
    return str;
}   

【问题讨论】:

  • Docs?
  • 你觉得str.charAt()没有任何争论应该怎么做?
  • 给我一个错误,我应该提出一个论点,但我很困惑和迷失。

标签: java string charat


【解决方案1】:

我的解决方案使用concat 构建str2,并使用trim 删除最后一个空格。

public static String split(String str) {
     String str2 = "";
     for(int i=0; i<str.length(); i++) {
        str2 = str2.concat(str.charAt(i)+" ");
     }
     return str2.trim();
}

【讨论】:

    【解决方案2】:
    1. 您不修改方法参数,而是复制它们。
    2. 您不会在循环内进行空检查/空检查,而是在方法中首先执行此操作。
    3. for loop 中的标准是i &lt; size,而不是size &gt; i...嗯

      /**
       * Splits the string str into individual characters: Small becomes S m a l l
       */
      public static String split(final String str) 
      {
          String result = "";
      
          // If parameter is null or empty, return an empty string
          if (str == null || str.isEmpty())
              return result;
      
          // Go through the parameter's characters, and modify the result
          for (int i = 0; i < str.length(); i++) 
          {
              // The new result will be the previous result,
              // plus the current character at position i,
              // plus a white space.
              result = result + str.charAt(i) + " ";  
          }
      
          return result;
      }   
      


    4. 专业版,使用StringBuilder 作为结果,使用静态最终常量作为空字符串和空格字符。

    和平!

    【讨论】:

    • 0_0 这就像从 java 大师传给新手的信息。谢谢,但你能向我解释一下 for 循环。所以首先我们将 int i 声明为 0。然后我们说如果 i 小于 str.length(),则增加 i。所以当 i 大于 str.length 时,这个循环就会停止,对吧?
    • 是的,这正是它的工作原理。假设您有字符串small。这意味着i 将变为0, 1, 2, 3, 4(因为small 有5 个字符)。因此,在每次迭代中,您的代码将类似于result=result+str.charAt(0)+" ";,然后是result=result+str.charAt(1)+" ";,等等。
    • 代码str.charAt(i) 方法只告诉您在字符串str 中的i 位置找到了什么字符。而已。请注意,字符串中的字符数从 0 开始!!!
    • 专业人士,使用正则表达式:String result = "small".replaceAll("\\B", " ") 或更简单,没有正则表达式 "small".replace("", " ").trim() :)
    • @Pshemo 这超出了专业人士的范围。好吧,我想我们每天都会学到一些东西。关键是,老兄想知道charAt(int)
    【解决方案3】:

    问自己一个问题,s来自哪里?

    char space = s.charAt(); ??? s ???
    

    第二个问题,人物在?

    public static String split(String str){
        for(int i = 0; i < str.length(); i++) {
            if (str.length() > 0) {
                char space = str.charAt(i)
            }
        }
        return str;
    }
    

    【讨论】:

    • 在 Eclipse 中它不会让我有你的第一行 char space = str.charAt();,它说我应该在参数中放一些东西
    • 第一行是为了说明你犯的错误。您试图调用不存在的 s。其次,您不在 charAt 方法中使用索引。只需删除 'char space = s.charAt(); ??? s???'
    【解决方案4】:

    @Babanfaraj,这是像你这样的新手的回答!! 代码非常简单。更正后的程序是-

    class fopl
    {
        public static void main(String str) 
        {
        int n=str.length();
            for (int i = 0;i<n; i++) 
        {
            if (n>=0) 
            {
                String space = str.charAt(i)+" ";
                System.out.print(space);
            }
        }
    }   
    }
    

    很高兴为您提供帮助!

    【讨论】:

      猜你喜欢
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-11
      • 2011-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多