Array

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

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

 

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.