Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

how to get an array as input in java

    public class TakingInput {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        System.out.println("enter number of elements");

        int n=s.nextInt();

        int arr[]=new int[n];

        System.out.println("enter elements");

        for(int i=0;i<n;i++){//for reading array
            arr[i]=s.nextInt();

        }

        for(int i: arr){ //for printing array

            System.out.println(i);

        }


    }

}
Comment

array input java

int n;  
Scanner sc=new Scanner(System.in);  
System.out.print("Enter the number of elements you want to store: ");  
//reading the number of elements from the that we want to enter  
n=sc.nextInt();  
//creates an array in the memory of length 10  
int[] array = new int[10];  
System.out.println("Enter the elements of the array: ");  
for(int i=0; i<n; i++)  
{  
//reading array elements from the user   
array[i]=sc.nextInt();  
}  
Comment

PREVIOUS NEXT
Code Example
Java :: scanner.hasnext() 
Java :: java stack methods 
Java :: android java listview clean 
Java :: mongodb java find all documents 
Java :: from date to string 
Java :: java delete column from csv 
Java :: android double click to exit 
Java :: okhttp3 library in android studio 
Java :: java declare a list 
Java :: hello world! java 
Java :: java run code at interval 
Java :: java split string array 
Java :: setbackground java 
Java :: generating random number 
Java :: java map get key from value 
Java :: java print formatted string 
Java :: big java 2010 heaptree java program 
Java :: find java_home path in cmd 
Java :: String remove duplicate method in java 
Java :: mockito verify more than once 
Java :: stringbuffer in java 
Java :: insertion sort in java 
Java :: set view layout params android 
Java :: java list to array 
Java :: first character string number java 
Java :: min in java 
Java :: java string get ith char 
Java :: in java how to compare two strings 
Java :: recursion examples java 
Java :: spark write partitionby 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =