Answer to Question #261116 in Java | JSP | JSF for hassan

Question #261116

(a)  Write Java code for implementing arrays that accepts user inputs and then displays them. Take different arrays like this: stringArray, integerArray, doubleArray.



1
Expert's answer
2021-11-04T16:28:42-0400
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]+" ");
	    }
	}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS