Ask the user for an arrayy size. Then receive this number of character values. P
ID: 3631627 • Letter: A
Question
Ask the user for an arrayy size. Then receive this
number of character values. Place the values within a
char array, i.e. the first array.
• Now ask the user for another array size. Copy the
first array to this new array. If the new size is smaller
than the original, then only copy this number (i.e.
the second array s’s size) from the first array.
• If the new size is larger than the original, copy the
whole array to the second array, and then ask the
user for additional characters.
• Print both arrays
Explanation / Answer
import java.io.*;
import java.util.*;
public class CharArray
{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(System.in);
//variables to hold characters
char[] ch1;
char[] ch2;
//variables to read the size of arrays
int num1, num2;
System.out.println("Enter number of characters in the first array : ");
//read first array size
num1 = input.nextInt();
//creating array of given size
ch1 = new char [ num1 ];
System.out.println("Enter "+ num1 +" characters into the first array... ");
for (int i=0; i<num1; i++)
{
System.out.println("Enter character "+ (i+1) +": ");
String str = input.next();
//reading characters into the first array
ch1[i]=str.charAt(0);
}
System.out.println("Enter number of characters in the second array : ");
num2 = input.nextInt();
//creating array of given size
ch2 = new char [ num2 ];
//condition to check the array sizes
if (num2<=num1)
{
for (int i=0; i<num2; i++)
//copy elements in first array to second array
ch2[i]=ch1[i];
System.out.println("The characters in the first array : ");
//display out put
for (int i=0; i<num1; i++)
System.out.println(" "+ch1[i]);
System.out.println("The characters in the Second array : ");
for (int i=0; i<num2; i++)
System.out.println(" "+ch2[i]);
}
else
{
int i;
for (i=0; i<num1; i++)
//copy elements in first array to second array
ch2[i]=ch1[i];
System.out.println("Enter "+ (num2-num1) +" more characters into the second array...");
for (i=num1;i<num2; i++)
{
System.out.println("Enter character "+ (i+1) +": ");
String str = input.next();
//reading extra characters into the second array
ch2[i]=str.charAt(0);
}
//display out put
for (i=0; i<num1; i++)
System.out.println(" "+ch1[i]);
System.out.println("The characters in the Second array : ");
for (i=0; i<num2; i++)
System.out.println(" "+ch2[i]);
}
}
}
Output:
Sample run1:
Enter number of characters in the first array :
2
Enter 2 characters into the first array...
Enter character 1:
a
Enter character 2:
b
Enter number of characters in the second array :
3
Enter 1 more characters into the second array...
Enter character 3:
c
The characters in the First array :
a
b
The characters in the Second array :
a
b
c
Sample run2:
Enter number of characters in the first array :
5
Enter 5 characters into the first array...
Enter character 1:
a
Enter character 2:
b
Enter character 3:
c
Enter character 4:
d
Enter character 5:
e
Enter number of characters in the second array :
3
The characters in the first array :
a
b
c
d
e
The characters in the Second array :
a
b
c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.