You are here: Home / Topics / Read array from keyboard in Java

Read array from keyboard in Java

Filed under: Java on 2023-08-19 18:38:03

//  Read array from keyboard

import java.util.*;

class  ArrayTest
{
public static void main( String args[ ] )
{
 Scanner sc = new Scanner( System.in );
 int  i;
 int  a[ ] = new int[5];
 
 for( i=0 ; i<5 ; i++ )
 {
  System.out.print( "Enter Value : " );
  a[ i ] = sc.nextInt();
 }
 
 System.out.print("\n Array elements are : ");
 for( i=0 ; i<5 ; i++ )
 {
  System.out.print( " " + a[ i ] );
 }
}
}

 

Output:

Enter Value : 10
Enter Value : 20
Enter Value : 30
Enter Value : 40
Enter Value : 50

Array elements are : 10  20  30  40  50

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.