当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Merging 2 strings

问题描述:

Hi I'm supposed to write a program that takes two sentences from the user, The program then merges the sentence as shown:

Enter sentence1: hello hope you are fine

Enter sentence2: this is good

hello this hope is you good are fine

I can only make use of String api to solve this code.

Currently my code returns the error

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)

Here is my code:

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

}

}

网友答案:

To make a program, divide it in little tasks:

  • Divide Strings in Arrays
  • Find Larger array
  • Append one word from each Array until finish shortest
  • get rest of other String
  • append to result

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

OUTPUT:

hello this hope is you good are fine

Just replace the String definition by Scanners...

分享给朋友:
您可能感兴趣的文章:
随机阅读: