Table of Contents
In this article, we will discuss the concept of Java program to get array input from user using for loop
In this post, we are going to learn how to write a program to take array input using for loop in Java language
In this code, we are going to learn how to input array of integer using for loop in Java language
Program 1
import java.util.Scanner; public class TakeArrayInputfor{ public static void main(String args[]){ //scanner class to read input from the user Scanner sc=new Scanner(System.in); //Declaring and creating array int[] arr=new int[6]; //Display default value after declaring System.out.println("Default values of given integer 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+" integer values"); for(int i=0; i<arr.length; i++){ arr[i]=sc.nextInt(); }//using the for loop to initializing array //displaying the array System.out.println("\n******displaying array elements******"); System.out.println("Entered array elements 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 value of given integer array: 0 0 0 0 0 0 *****initializing array***** Enter 6 integer values 123 234 345 456 567 678 *****displaying array elements***** Entered array elements are 123 234 345 456 567 678
In this code, we are going to learn how to input array of Strings using for loop in Java language
Program 2
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 string array System.out.println("Default values of given String array: "); for(int i=0; i<=arr.length-1; i++){ System.out.print(arr[i]+"\t"); }//find default value of the given array using for loop System.out.println("");//move to next line //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-1; i++){ arr[i]=sc.nextLine(); //Take input through the for loop }//using the for loop to initializing array //displaying the array elements System.out.println("\n******displaying array of Strings******"); System.out.println("Entered Strings are"); for(int i=0; i<=arr.length-1; i++){ System.out.print(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 Strings value Suna Soma Kuna Jana ******displaying array of Strings****** Entered Strings are Suna Soma Kuna Jana
In this code, we are going to learn how to input array of characters using for loop in Java language
Program 3
import java.util.Scanner; public class TakeCharArrayInputfor{ 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 character values"); arr=sc.next().toCharArray(); //Take character input to array from the user //displaying the array elements System.out.println("\n******displaying array of characters******"); System.out.println("Entered Characters are"); for(int i=0; i<arr.length; i++){ System.out.print(arr[i]+"\t"); } } }
When the above code is executed,, it produces the following result
******Initializing array****** Enter character values char ******displaying array of characters****** Entered characters are: c h a r
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.