合并2个字符串

时间:2015-10-07 14:50:36

标签: java

您好我应该编写一个程序,从用户那里拿两句话,程序然后合并句子如下所示:

  

输入句子1:你好希望你没事

     

输入句子2:这很好

     

你好,希望你好,很好

我只能使用String api来解决这段代码。

目前我的代码返回错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1955)
    at Test.main(Test.java:20)

这是我的代码:

import java.util.*;

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

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter sentence 1: ");
        String sentence1 = sc.nextLine();
        System.out.print("Enter sentence 2: ");
        String sentence2 = sc.nextLine();

        String combinedSentence = "";

        do {

            if (sentence1.indexOf(" ") > 0 && sentence2.indexOf(" ") > 0) {
                combinedSentence = combinedSentence + " " + sentence1.substring(0, sentence1.indexOf(" "));
                combinedSentence = combinedSentence + " " + sentence2.substring(0, sentence2.indexOf(" "));
            } else if (sentence1.indexOf(" ") < 0) {
                combinedSentence = combinedSentence + " " + sentence1;
                combinedSentence = combinedSentence + " " + sentence2.substring(0, sentence2.indexOf(" "));
            } else if (sentence2.indexOf(" ") < 0) {
                combinedSentence = combinedSentence + " " + sentence1.substring(0, sentence1.indexOf(" "));
                combinedSentence = combinedSentence + " " + sentence2;
            }

            sentence1 = sentence1.substring(sentence1.indexOf(" ") + 1);
            sentence2 = sentence2.substring(sentence2.indexOf(" ") + 1);

        } while (sentence1.isEmpty() != true && sentence2.isEmpty() != true);



    }
}

1 个答案:

答案 0 :(得分:0)

制作一个程序,将其分成小任务:

  • Strings
  • 中划分Arrays
  • 查找更大的数组
  • 从每个Array附加一个字,直至完成最短
  • 获取其他String
  • 的其他内容
  • 追加到结果

public static void main(String[] args)  {
    String SPACE = " ";

    // Divide strings in arrays
    String a = "hello hope you are fine";
    String b = "this is good";

    String[] aa = a.split(" ");
    String[] bb = b.split(" ");

    //Find Larger array
    int end = aa.length > bb.length ? bb.length : aa.length;

    StringBuilder sb = new StringBuilder(); 

    //Append one word from each array until finish shortest
    for (int i = 0; i < end; i++) {
         sb.append(aa[i]);
         sb.append(SPACE);
         sb.append(bb[i]);
         sb.append(SPACE);
    }

     // get rest of other string
    String remaining = "";
    if (aa.length > bb.length) {
        // this oneliner is long, weird and uses Arrays library in purpose
        // it can be much more easy using String.substring and String.indexOf
        // YOU MUST MODIFY IT
        remaining = Arrays.toString(Arrays.copyOfRange(aa, bb.length, aa.length)).replace(",", "");
    } else {
        remaining = Arrays.toString(Arrays.copyOfRange(bb, aa.length, bb.length)).replace(",", "");
    }

    // append to result 
    sb.append(remaining.substring(1,remaining.length()-1));

    //when fixing the oneliner below will be just:
    sb.append(remaining);

    // output
    System.out.println(sb);
}   

<强>输出:

hello this hope is you good are fine

只需将String定义替换为Scanners ...