(a) Write Java code for implementing arrays that accepts user inputs and then displays them. Take different arrays like this: stringArray, integerArray, doubleArray.
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
//string array
int n1;
System.out.print("Enter number of elements of string array: ");
n1=in.nextInt();
String [] stringArray = new String [n1];
System.out.println("Enter elements of the string array:");
for(int i=0;i<n1;i++){
stringArray[i]=in.next();
}
//int array
int n2;
System.out.print("Enter number of elements of integer array: ");
n2=in.nextInt();
int [] integerArray = new int[n2];
System.out.println("Enter elements of the integer array:");
for(int i=0;i<n2;i++){
integerArray[i]=in.nextInt();
}
//double array
int n3;
System.out.print("Enter number of elements of double array: ");
n3=in.nextInt();
double [] doubleArray = new double [n3];
System.out.println("Enter elements of the double array:");
for(int i=0;i<n3;i++){
doubleArray[i]=in.nextDouble();
}
//display string array
System.out.println("Elements of string array are: ");
for(int i=0;i<n1;i++){
System.out.print(stringArray[i]+" ");
}
//display int array
System.out.println("\nElements of int array are: ");
for(int i=0;i<n2;i++){
System.out.print(integerArray[i]+" ");
}
//display double array
System.out.println("\nElements of double array are: ");
for(int i=0;i<n3;i++){
System.out.print(doubleArray[i]+" ");
}
}
}
Comments
Leave a comment