Take String Array input in Java language
Take String Array input in Java language
In this tutorial, we will discuss the concept of Take String Array input in Java language
In this topic, we are going to learn how to take string array input in Java programming language using for, while and do-while loops.
In this blog, We have already discuss that “What is an array“, type of arrays and how to access it(one dim, two dim and three dim arrays)
Arrays are the special type of variables to store Multiple type of values(int, string,char, etc)under the same name in the continuous memory location. we can be easily accessed using the indices(indexes)
Here,
- s1,s2,s3,s4 and s5 represent the string elements of the array
- 0,1,2,3 and 4 represent the index of the array which is used to access the array elements
- 1,2,3,4 and 5 represent the length of the array
Code to fill string array elements of array
Program to fill String in array using for loop
In this program, we are briefing how to take input String elements of an array using for loop in Java language
Program 1
import java.util.Scanner; public class TakeStringArrayInputfor{ 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: "); for(int i=0; i<arr.length; i++){ System.out.println(arr[i]+"\t"); }//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"); for(int i=0; i<arr.length; i++){ arr[i]=sc.nextLine(); }//using the for loop to initializing array //displaying the array elements System.out.println("\n******displaying array elements******"); System.out.println("Entered Strings are"); for(int i=0; i<arr.length; i++){ System.out.println(arr[i]+"\t"); } } }
When the above code is executed, it produces the following result
Default values of given String array: null null null null ******Initializing array****** Enter 4 String value Ani Abi Kavi Puvi ******displaying array elements****** Entered Strings are Ani Abi Kavi Puvi
Program to fill String array using while loop
In this program, we are briefing how to input String elements of an array 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){ System.out.println(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){ 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 array elements are"); i=0; while(i<arr.length){ System.out.println(arr[i]+"\t"); i++; } } }
When the above code is executed, it produces the following result
Default values of given String array: null null null null ******Initializing array****** Enter 4 String value Krish Muru Saman Sonu ******displaying array elements****** Entered Strings are Krish Muru Saman Sonu
Program to fill String in an array using do-while loop
In this program, we are briefing how to input String elements of an array using do-while loop in Java language
Program 3
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.println(arr[i]+"\t"); i++; }while(i<arr.length); //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; do{ arr[i]=sc.nextLine(); i++; }while(i<arr.length); //using the do-while loop to initializing array //displaying the array elements System.out.println("\n******displaying array elements******"); System.out.println("Entered Strings are are"); i=0; do{ System.out.println(arr[i]+"\t"); i++; }while(i<arr.length); } }
When the above code is executed, it produces the following result
Default values of given String array: null null null null null ******Initializing array****** Enter 5 String value Sivaji Pirapu Kamal Rajani Ajith ******displaying array elements****** Entered Strings are Sivaji Pirapu Kamal Rajani Ajith
Similar post
Code to read and print integers of an array in Java
Code to read and print strings of an array in Java
Code to read and print characters of an array in Java
Code to read and print integers of an array in C++
Code to read and print strings of an array in C++
Code to read and print characters of an array in C++
Code to read and print integers of an array in C
Code to read and print strings of an array in C
Suggested for you
One dimensional array in C language
One dimensional array in C++ language
Two dimension array in Java language
Two dimension array in C language
Two dimension array in C++ language
Three dimension array in Java language
Three dimension array in C language
Three dimension array in C++ language
for loop in Java language
while loop in Java language
Do-while loop in Java language
Enhanced for loop in Java language
Data type and variable in Java language
Operator in Java language
1 Comment
anchal lama July 15, 2022 at 4:27 pm
when we also input the size of arrays in program’s from user side then why it would not print up the aspected array.
///code section
Scanner sc = new Scanner(System.in);
System.out.print(“Input number of Student name: “);
int m = sc.nextInt();
String arr[] = new String[m];