Last active
March 26, 2019 14:50
-
-
Save davidauza-engineer/b51349e6a79b6e418e33394f6169f694 to your computer and use it in GitHub Desktop.
Console program in Java to concatenate two integer arrays created by the user.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
import java.util.Arrays; | |
public class ArrayConcatenation { | |
static Scanner sc = new Scanner(System.in); | |
static int[] arrayOne; | |
static int[] arrayTwo; | |
static int[] finalArray; | |
public static void main(String args[]) { | |
int arrayOneSize; | |
String arrayOneIdentifier = "first"; | |
int arrayTwoSize; | |
String arrayTwoIdentifier = "second"; | |
System.out.println("\nWelcome to the program to concatenate two arrays of integer numbers."); | |
arrayOneSize = requestArraySize(arrayOneIdentifier); | |
arrayOne = new int[arrayOneSize]; | |
fillArray(arrayOne, arrayOneIdentifier); | |
arrayTwoSize = requestArraySize(arrayTwoIdentifier); | |
arrayTwo = new int[arrayTwoSize]; | |
fillArray(arrayTwo, arrayTwoIdentifier); | |
concatenate(arrayOne, arrayTwo); | |
} | |
/** | |
* This method requests the user to determine the size of an array | |
* | |
* @param pArrayIdentifier String used to let the user know which array size is going to determine | |
* @return Returns the size of the array determined by the user. | |
*/ | |
private static int requestArraySize(String pArrayIdentifier) { | |
boolean validSize = false; | |
int numberToReturn = 0; | |
String error = "\nError. The size must be an integer number greater than 0."; | |
while (!validSize) { | |
System.out.println("\nPlease insert the size of the " + pArrayIdentifier + " array:"); | |
if (sc.hasNextInt()) { | |
numberToReturn = sc.nextInt(); | |
if (numberToReturn > 0) { | |
validSize = true; | |
} else { | |
// The user entered an integer number less than or equal to 0 | |
System.out.println(error); | |
} | |
} else { | |
// The user did not enter an integer number | |
System.out.println(error); | |
} | |
sc.nextLine(); | |
} | |
return numberToReturn; | |
} | |
/** | |
* This method fills each position of the array with the number specified by the user | |
* | |
* @param pArray The array to fill | |
* @param pArrayIdentifier The String to let the user know which array is working with | |
*/ | |
private static void fillArray(int[] pArray, String pArrayIdentifier) { | |
boolean validInteger = false; | |
for (int i = 0; i < pArray.length; i++) { | |
System.out.println("\nPlease insert the value for position " + i + " of the " + pArrayIdentifier + " array:"); | |
while (!validInteger) { | |
if (sc.hasNextInt()) { | |
pArray[i] = sc.nextInt(); | |
validInteger = true; | |
} else { | |
System.out.println("Error. Not a valid integer number."); | |
} | |
sc.nextLine(); | |
} | |
validInteger = false; | |
} | |
System.out.println("\nThe " + pArrayIdentifier + " array elements are: " + Arrays.toString(pArray)); | |
} | |
/** | |
* This method concatenates the arrays previously created by the user. | |
* | |
* @param pArrayOne The first array to concatenate. | |
* @param pArrayTwo The second array to concatenate. | |
*/ | |
private static void concatenate(int[] pArrayOne, int[] pArrayTwo) { | |
int arrayOneLength = pArrayOne.length; | |
int arrayTwoLength = pArrayTwo.length; | |
// This variable keeps track of the index already filled with a number | |
int counter = 0; | |
finalArray = new int[arrayOneLength + arrayTwoLength]; | |
for (int i = 0; i < arrayOneLength; i++) { | |
finalArray[i] = pArrayOne[i]; | |
counter = i; | |
} | |
// One is added to the counter, so we can start filling the next available index at the array | |
counter++; | |
for (int i = 0; i < arrayTwoLength; i++) { | |
finalArray[counter] = pArrayTwo[i]; | |
counter++; | |
} | |
System.out.println("\nThe final array is: " + Arrays.toString(finalArray)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment