Java program to get array input from user using for loop

Java program to get array input from user using for loop

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

Code to take  array input from user

Code to take integer array input using for loop – #1

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

 

 

Code to take string array input using for loop – #2

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


Code to take char array input using for loop – #3

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

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

4 weeks ago

PHP Star triangle Pattern program

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

4 weeks 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.