Table of Contents
Java program to accept array input from user using do- while loop
In this article, we will discuss the concept of Java program to accept array input from user using do- while loop
In this post, we are going to learn how to write a program to input elements of an array using do-while loop in Java language
Code to take array input from user
Code to take integer array input using do-while loop – #1
In this code, we are going to learn how to take input array of integer using do-while loop in Java language
Program 1
import java.util.Scanner;
public class TakeArrayInputDowhile{
public static void main(String args[]){
//scanner class object to read input
Scanner sc=new Scanner(System.in);
//Declaring and creating array
int[] arr=new int[6];
//Display dfault value before initialize
System.out.println("Default value of integer array: ");
int i=0;
do{//print default value of integer array using do-while
System.out.println(arr[i]+"\t");
i++;
}while(i<arr.length);
System.out.println("");//move to next line
//initializing the array
System.out.println("******Initializing array******");
System.out.println("Enter "+arr.length+" integer values");
i=0;
do{//accept array input using do-while
arr[i]=sc.nextInt();
i++;
}while(i<arr.length);
//displaying the array
System.out.println("\n******displaying array elements******");
System.out.println("Entered array elements are");
i=0;
do{//print array elements using do-while
System.out.println(arr[i]+"\t");
i++;
}while(i<arr.length);
}
}
When the above code is executed, it produces the following result
Default value of integer array: 0 0 0 0 0 0 ******Initializing array****** Enter 6 integer values 111 222 333 444 555 666 ******displaying array elements****** Entered array elements are 111 222 333 444 555 666
Code to take String array input using do-while loop – #2
In this code, we are going to learn how to input array of strings using do-while loop in Java language
Program 2
import java.util.Scanner;
public class TakeStringArrayInputDoWhile{
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[5];
//Display default value after declaring
System.out.println("Default values of given String array: ");
int i=0;
do{
System.out.print(arr[i]+"\t");
i++;
}while(i<=arr.length-1);
//find default String value of the given array
System.out.println("");
//initializing String value to the array
System.out.println("******Initializing array******");
System.out.println("Enter "+arr.length+" String values");
i=0;
do{
arr[i]=sc.nextLine();
i++;
}while(i<=arr.length-1);
//using the do-while loop to initializing array
//displaying the array elements
System.out.println("\n******displaying array of Strings******");
System.out.println("Entered Strings are");
i=0;
do{
System.out.print(arr[i]+"\t");
i++;
}while(i<=arr.length-1);
}
}
When the above code is executed, it produces the following result
Default value of given String array null null null null null ******Initializing array****** Enter 5 String values Java php Python Ruby Ada ******displaying array of Strings****** Entered Strings are Java php Python Ruby Ada
Code to take character array input using do-while loop – #3
In this code, we are going to learn how to input array of characters using do-while loop in Java language
Program 3
import java.util.Scanner;
public class TakeCharArrayInputdowhile{
public static void main(String args[]){
//scanner object to read input from the user
Scanner sc=new Scanner(System.in);
//Declaring and creating character array
char[] arr=new char[5];
//initializing value to the array
System.out.println("******Initializing array******");
System.out.println("Enter characters");
arr=sc.next().toCharArray();
//Take character input from user
//displaying the array elements
System.out.println("\n******displaying characters in an array******");
System.out.println("Entered Characters are");
int i=0;
do{
System.out.print(arr[i]+"\t");
i++;
}while(i<arr.length);
}
}
When the above code is executed, it produces the following result
******Initializing array****** Enter characters Coding ******displaying characters in an array****** Entered characters are C o d i n g
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