【问题标题】:Creating a method with strings and loops使用字符串和循环创建方法
【发布时间】:2013-10-13 21:26:33
【问题描述】:

我正在尝试创建一个方法,该方法将采用两个字符串并返回一个字符串,该字符串已将字符串 1 中括号之间的单词替换为字符串 2 中括号中的单词,但我遇到了一些我看不到的问题去理解。举个例子

replaceText("a (simple) programming (example)", "(cool) (problem)") 

应该返回

"a cool programming problem" 

replaceText("a ((nested) example) with (three) replacements (to (handle))", 
            "the replacements are (answer) and (really (two) not three)") 

应该返回

"an answer with really (two) not three replacements " 

我只能使用循环、基本的 String 方法(.length()、.charAt())、基本的 StringBuilder 方法和 Character 方法,但我遇到了严重的困难。

现在这是我的代码

public class loopStringAnalysis {

public static String replaceText (String s1, String s2){
StringBuilder newStringBuild = new StringBuilder ();
int count = 0;
int count1 = 0;
for (int i = 0, i1 = 0; i < s1.length() && i1 < s2.length(); i = i + 1){
  if (s1.charAt(i) == '(')
    count = count + 1;
  else if (s1.charAt(i) == ')')
    count = count - 1;
  else if (count == 0)
    newStringBuild.append(s1.charAt(i));
  else if (count != 0){
    while (count1 == 0) {
      if (s2.charAt(i1) == '(')
        count1 = count1 + 1;
      else if (s2.charAt(i1) == ')') 
        count1 = count1 - 1; 
      i1 = i1 + 1;
    }
    while (count1 != 0) {
      if (s2.charAt(i1) == '(')
        count1 = count1 + 1;
      else if (s2.charAt(i1) == ')')
        count1 = count1 - 1;
      else if (count1 != 0)
        newStringBuild.append(s2.charAt(i1));
      i1 = i1 + 1;
    }
    while (count != 0) {
      if (s1.charAt(i) == '(')
        count = count + 1;
      else if (s1.charAt(i) == ')')
        count = count - 1;
      i = i + 1;
    }
  }
}
return newStringBuild.toString();
}   

对于第一个示例,这将返回“一个很酷的编程项目”,而对于第二个示例,这将返回“一个答案,实际上是两个而不是三个”。我知道这种方法有问题,但我似乎不知道在哪里。感谢您对修复代码的任何帮助。

【问题讨论】:

  • 我在debug strategy 上有一些注释,如果您想自己找到错误,可能会发现这些注释很有用。

标签: java string loops stringbuilder


【解决方案1】:

我认为到目前为止您编写的代码过于复杂。不用遍历字符串来寻找开始和结束括号,而是使用 String 方法

String.indexOf(char c)

查找括号的索引。这样您就不必遍历第一个字符串。如果您不能使用此方法,请告诉我,我可以尝试提供更多帮助。

【讨论】:

  • 很遗憾我不能。我仅限于使用 String 的 charAt 和 length 方法,StringBuilder 的 append、charAt、setCharAt、length 和 toString 方法以及字符类中的方法。
【解决方案2】:

试试这个:

public class loopStringAnalysis {

    public static String replaceText(String s1, String s2) {
        StringBuilder newStringBuild = new StringBuilder();
        int count = 0;
        int count1 = 0;
        for (int i = 0, i1 = 0; i < s1.length() && i1 < s2.length(); i = i + 1) {
            if (s1.charAt(i) == '(') {
                count = count + 1;
            } else if (s1.charAt(i) == ')') {
                count = count - 1;
            } else if (count == 0) {
                newStringBuild.append(s1.charAt(i));
            } else if (count != 0) {
                while (count1 == 0) {
                    if (s2.charAt(i1) == '(') {
                        count1 = count1 + 1;
                    } else if (s2.charAt(i1) == ')') {
                        count1 = count1 - 1;
                    }
                    i1 = i1 + 1;
                }
                while (count1 != 0) {
                    if (s2.charAt(i1) == '(') {
                        count1 = count1 + 1;
                    } else if (s2.charAt(i1) == ')') {
                        count1 = count1 - 1;
                    }
                    if (count1 != 0) {
                        newStringBuild.append(s2.charAt(i1));
                    }
                    i1 = i1 + 1;
                }
                while (count != 0) {
                    i = i + 1;
                    if (s1.charAt(i) == '(') {
                        count = count + 1;
                    } else if (s1.charAt(i) == ')') {
                        count = count - 1;
                    }
                }
            }
        }
        return newStringBuild.toString();
    }

    public static void main(String [] args){
        System.out.println(replaceText("a ((nested) example) with (three) replacements (to (handle))",
            "the replacements are (answer) and (really (two) not three)"));
    }
}

让我知道这是否有效?

【讨论】:

  • 这对于我提供的第一个示例非常有效,但第二个示例仅返回“一个真正(二)而不是三个的答案”而不是“一个真正(二)不是三个替换的答案”。无论如何感谢您的帮助!!!
  • 我真的不喜欢纯代码答案。您能否解释一下为什么这将是一个理想的解决方案?
  • 感谢您的帮助,我已经完成了!!!只需对条件进行一些更改,现在它就可以完美运行了。谢谢!! P.S:我不明白你为什么投反对票:(
  • 很高兴知道 :),我也不知道 downvote :(。但这是个好问题。我赞成你的问题。
猜你喜欢
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 2013-01-08
  • 1970-01-01
  • 2014-07-06
  • 2013-04-14
相关资源
最近更新 更多