Table of Contents
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
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
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.