eriezelagera

Concatinate [2] Arrays

Aug 30th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. import java.io.*;
  2. import javax.swing.*;
  3.  
  4. /**
  5.  * Create a function that add two input arrays with the same
  6.  * number of elements but the output will be alternate letter
  7.  * from both arrays.
  8.  */
  9. public class ConcatArrays {
  10.  
  11.     public ConcatArrays() throws IOException {
  12.         char[] input1;
  13.         char[] input2;
  14.        
  15.         do {
  16.             BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  17.             System.out.print("Enter for input1: ");
  18.             input1 = br.readLine().toCharArray();
  19.  
  20.             System.out.print("Enter for input2: ");
  21.             input2 = br.readLine().toCharArray();
  22.            
  23.             if (input1.length != input2.length) {
  24.                 System.out.println("No of elements not matched...");
  25.             }
  26.         } while (input1.length != input2.length);
  27.        
  28.         System.out.println("Output: " + alterCaseInput(input1, input2));
  29.     }
  30.    
  31.     /**
  32.      * Create an alternate case letter from two arrays.
  33.      * <i>
  34.      * Example: <br />
  35.      * input1: <b>AAA<b/> <br />
  36.      * input2: <b>BBB<b/> <br />
  37.      * Output: <b>ABABAB</b>
  38.      * </i>
  39.      * @param input1 Array 1
  40.      * @param input2 Array 2
  41.      * @return Concatenated two arrays
  42.      */
  43.     private String alterCaseInput(char[] input1, char[] input2) {
  44.         String output = "";
  45.         int i = 0;
  46.         while (true) {
  47.             if (i < input1.length) {
  48.                 output += input1[i];
  49.             }
  50.             if (i < input2.length) {
  51.                 output += input2[i];
  52.             }
  53.             if (i >= input1.length && i >= input2.length) {
  54.                 break;
  55.             }
  56.             i++;
  57.         }
  58.         return output;
  59.     }
  60.    
  61.     public static void main(String args[]) {
  62.         SwingUtilities.invokeLater(new Runnable() {
  63.  
  64.             @Override
  65.             public void run() {
  66.                 try {
  67.                     new ConcatArrays();
  68.                 } catch (IOException e) {
  69.                     System.out.println("[WARNING] " + e.getLocalizedMessage());
  70.                 }
  71.             }
  72.         });
  73.     }
  74.    
  75. }
Advertisement
Add Comment
Please, Sign In to add comment