Table of Contents
Program to fill array of elements from user using while loop in Java
In this article, we will discuss the concept of Program to fill array of elements from user using while loop in Java
In this post, we are going to learn how to write a program to input elements of an array using while loop in Java language
Code to take array input from user
Code to take integer array input using while loop – #1
In this code, we are going to learn how to take input array of integer using while loop in Java language
Program 1
import java.util.Scanner; public class TakeArrayInputwhile{ public static void main(String args[]){ //scanner class object to read input Scanner sc=new Scanner(System.in); //Declaring and creating integer array int[] arr=new int[6]; //Display default value before initialize System.out.println("Default value of integer array: "); int i=0; while(i<arr.length){ System.out.println(arr[i]+"\t"); i++; } System.out.println(""); //initializing integer array System.out.println("******Initializing array******"); System.out.println("Enter "+arr.length+" integer values"); i=0; while(i<arr.length){//initializing array elements using while loop arr[i]=sc.nextInt(); i++; } //displaying the array values System.out.println("\n******displaying array elements******"); System.out.println("Array elements are"); i=0; while(i<arr.length){//display array elements using while loop System.out.println(arr[i]+"\t"); i++; } } }
When the above code is executed, it produces he following result
Default value of integer array: 0 0 0 0 0 0 "******Initializing array******" Enter 6 integer values 321 432 543 654 765 876 ******displaying array elements****** Array elements are 321 432 543 654 765 876
Code to take String array input using while loop – #2
In this code, we are going to learn how to take input array of Strings using while loop in Java language
Program 2
import java.util.Scanner; public class TakeStringArrayInputWhile{ public static void main(String args[]){ //scanner class to read input from the user Scanner sc=new Scanner(System.in); //Declaring and creating String array String[] arr=new String[4]; //Display default value after declaring System.out.println("Default values of given String array: "); int i=0; while(i<=arr.length-1){ System.out.print(arr[i]+"\t"); i++; }//find default value of the given array System.out.println(""); //initializing value to the array System.out.println("******Initializing array******"); System.out.println("Enter "+arr.length+" String values"); i=0; while(i<=arr.length-1){ arr[i]=sc.nextLine(); i++; }//using the for loop to initializing array //displaying the array elements System.out.println("\n******displaying array elements******"); System.out.println("Entered Strings are"); i=0; while(i<=arr.length-1){ System.out.print(arr[i]+"\t"); i++; } } }
When the above code is executed, it produces he following result
Default value of given String array: null null null null ******Initializing array****** Enter 4 string values Array coding for while ******displaying array elements****** Entered Strings are Array coding for while
Code to take character array input using while loop – #3
In this code, we are going to learn how to take input array of characters using while loop in Java language
Program 1
import java.util.Scanner; public class TakeCharArrayInputwhile{ public static void main(String args[]){ //scanner object to read input from the user Scanner s=new Scanner(System.in); //Declaring and creating character array char[] a=new char[5]; //initializing value to the array System.out.println("******Initializing array******"); System.out.println("Enter characters"); a=s.next().toCharArray(); //Take character input from user //displaying the array elements System.out.println("\n******displaying array of characters******"); System.out.println("Entered Characters are"); int i=0; while(i<a.length){ System.out.print(a[i]+"\t"); i++; } } }
When the above code is executed, it produces he following result
******Initializing array****** Enter characters Array ******displaying array of characters****** Entered Characters are A r r a y
Suggested for you
Do-while loop in Java language
Similar post
Java program to read and print array elements using for loop
Java program to read and print array elements using while loop
Java program to read and print array elements using Do-while loop
C code to take and print array elements using for loop
C code to take and print array elements using while loop
C code to take and print array elements using do-while loop
C++ program to read and print array elements using for loop
C++ program to read and print array elements using while loop
C++ program to read and print array elements using do-while loop