Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Program to fill array of elements from user using while loop in Java

Java program to accept array input from user using do- while loop

Posted on November 7, 2021November 7, 2021

Table of Contents

  • Java program to accept array input from user using do- while loop
    • Code to take  array input from user
      • Code to take integer array input using do-while loop – #1
      • Code to take String array input using do-while loop – #2
      • Code to take character array input using do-while loop – #3
    • Related

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

For loop in Java language

While loop in Java language

Do-while loop in Java language

Operator in Java language

Variable in Java language

Data type in Java language

class and main method in Java

One dimensional array in Java

 

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

 

Related

Recent Posts

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes