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
Language = Java

Respuesta :

import java.util.Scanner;

public class JavaApplication68 {

   

   public static void main(String[] args) {

       Scanner scan = new Scanner(System.in);

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

       String txt1 = scan.nextLine();

       String txt2 = scan.nextLine();

       String newTxt = "";

       if (txt1.length() != txt2.length()){

           System.out.println("error");

       }

       else{

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

               char c = txt1.charAt(i);

               char d = txt2.charAt(i);

               newTxt += c+""+d;

           }

           System.out.println(newTxt);

       }

       

   }

   

}

I hope this helps! BTW, I've completed a bunch of java problems similar to the ones you're posting now. If you want to check them out instead of spending your points, that might be something to look into.

The program encompasses the use of loops and conditional statements.

  • Loops are for repetitive operations
  • Conditional statements depend on certain conditions for their execution

The program in Java is as follows, where comments are used to explain each line.

import java.util.*;

public class Main {

   public static void main(String[] args) {

       //This creates a Scanner Object

       Scanner input = new Scanner(System.in);

       //This declares all variables

       String str1, str2, newStr = "error";

       //This prompts the user for inputs

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

       //This gets input for the first string

       str1 = input.nextLine();

       //This gets input for the second string

       str2 = input.nextLine();

       //This checks if the lengths of both strings are the same

       if (str1.length() == str2.length()){

           //If yes, this initializes the new string

           newStr = "";

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

               //The new string is generated

               newStr += str1.charAt(i)+""+str2.charAt(i);

           }

       }

       //This prints the required output

       System.out.println(newStr);

  }

}

At the end of the program, the appropriate output string is printed.

Read more about similar programs at:

https://brainly.com/question/19494183

ACCESS MORE