125k views
3 votes
JAVA

Take two String inputs of the same length and merge these by taking one character from each String (starting with the first entered) and alternating. If
the Strings are not the same length, the program should print "error".
Sample Run 1:
Enter Strings:
balloon
atrophy
baatlrloopohny

Sample Run 2:
Enter Strings:
terrible
mistake
error

User Hoonoh
by
4.9k points

1 Answer

5 votes

import java.util.Scanner;

public class JavaApplication83 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter Strings: ");

String word1 = scan.nextLine();

String word2 = scan.nextLine();

String newWord = "";

if (word1.length() == word2.length()){

for (int i = 0; i < word1.length(); i++)

{

newWord += word1.charAt(i) +""+word2.charAt(i);

}

}

else{

newWord = "error";

}

System.out.println(newWord);

}

}

I hope this helps!

User Kierchon
by
4.4k points