Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import javax.swing.*;
- /**
- * Create a function that add two input arrays with the same
- * number of elements but the output will be alternate letter
- * from both arrays.
- */
- public class ConcatArrays {
- public ConcatArrays() throws IOException {
- char[] input1;
- char[] input2;
- do {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Enter for input1: ");
- input1 = br.readLine().toCharArray();
- System.out.print("Enter for input2: ");
- input2 = br.readLine().toCharArray();
- if (input1.length != input2.length) {
- System.out.println("No of elements not matched...");
- }
- } while (input1.length != input2.length);
- System.out.println("Output: " + alterCaseInput(input1, input2));
- }
- /**
- * Create an alternate case letter from two arrays.
- * <i>
- * Example: <br />
- * input1: <b>AAA<b/> <br />
- * input2: <b>BBB<b/> <br />
- * Output: <b>ABABAB</b>
- * </i>
- * @param input1 Array 1
- * @param input2 Array 2
- * @return Concatenated two arrays
- */
- private String alterCaseInput(char[] input1, char[] input2) {
- String output = "";
- int i = 0;
- while (true) {
- if (i < input1.length) {
- output += input1[i];
- }
- if (i < input2.length) {
- output += input2[i];
- }
- if (i >= input1.length && i >= input2.length) {
- break;
- }
- i++;
- }
- return output;
- }
- public static void main(String args[]) {
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- try {
- new ConcatArrays();
- } catch (IOException e) {
- System.out.println("[WARNING] " + e.getLocalizedMessage());
- }
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment