使用字符串和循环创建方法

时间:2013-10-13 21:26:33

标签: java string loops stringbuilder

我正在尝试创建一个方法,该方法将接受两个字符串并返回一个字符串,该字符串已将字符串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();
}   

对于第一个例子,这将返回“一个coolprogramming项目”,而对于第二个例子,它返回“一个真正的两个而不是三个的答案”。我知道这种方法有问题,但我似乎无法想象在哪里。任何有关修复代码的帮助都表示赞赏。

2 个答案:

答案 0 :(得分:0)

我认为到目前为止您编写的代码不必要地复杂化。使用String方法

,而不是循环遍历查找开始和关闭parens的字符串
String.indexOf(char c)

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

答案 1 :(得分:-1)

试试这个:

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

如果有效,请告诉我?