Print array of strings java – Java Program to Take Input and Print an Array of String

Print array of strings java: In the previous article, we have seen Java Program to Print the Elements of an Array

In this article we are going to see how we can take input print an array of strings in Java. We will use the scanner class to take input.

Java Program to Take Input and Print an Array of String

How to print string array in java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to print an array of String.

Method-1: Java Program to Print String Array Using For Loop

Approach:

  • Ask the user to input the size and store it.
  • Create a string array of the specified size.
  • Use a for loop to store elements in the array.
  • Use another for loop to display all the strings.

Program:

import java.util.Scanner;
public class array
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for array size
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        String arr[] = new String[size];
        
        System.out.println("Enter the array elements : ");
        // Takes the string as input from the user
        for(int i = 0;i<size;i++)
        {
            arr[i] = scan.next();
        }

        System.out.println("The array elements are : ");
        // For loop to print the string elements
        for(int i = 0;i<size;i++)
        {
            System.out.println(arr[i]);
        }  
    }
}
Output:

Enter the array size : 5
Enter the array elements : Pune Hyderabad Bhubaneswar Kolkata Mumbai
The array elements are : 
Pune
Hyderabad
Bhubaneswar
Kolkata
Mumbai

Method-2: Java Program to Print String Array Using For-each Loop

Approach:

  • Ask the user to input the size and store it.
  • Create a string array of the specified size.
  • Use a for loop to store elements in the array.
  • Use another for-each loop to display all the strings.

Program:

import java.util.Scanner;
public class array
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for array size
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        String arr[] = new String[size];

        System.out.println("Enter the array elements : ");
        // Takes the string as input from the user
        for(int i = 0;i<size;i++)
        {
            arr[i] = scan.next();
        }

        System.out.println("The array elements are : ");
        // For-each loop to print the string elements
        for(String i : arr)
        {
            System.out.println(i);
        }  
    }
}

Output:

Enter the array size : 4
Enter the array elements : apple orange banana mango
The array elements are : 
apple
orange
banana
mango

Method-3: Java Program to Print Array Elements Using Arrays.toString()

Approach:

  • Ask the user to input the size and store it.
  • Create a string array of the specified size.
  • Use a for loop to store elements in the array.
  • Pass the array into the Arrays.toString( ) function.
  • Print the generated string.

Program:

import java.util.Scanner;
import java.util.Arrays;

public class array
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for array size
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        String arr[] = new String[size];

        System.out.println("Enter the array elements : ");
        // Takes the string as input from the user
        for(int i = 0;i<size;i++)
        {
            arr[i] = scan.next();
        }

        // Array elements converted to string and printed using toString()
        System.out.println("The array elements are : "+Arrays.toString(arr));
    }
}

Output:

Enter the array size : 7
Enter the array elements : red green black blue orange pink brown
The array elements are : [red, green, black, blue, orange, pink, brown]

Method-4: Java Program to Print Array Elements Using Java Stream API

Approach:

  • Ask the user to input the size and store it.
  • Create a string array of the specified size.
  • Use a for loop to store elements in the array.
  • Pass the array into the stream function and then use a for-each loop with it to print the elements.

Program:

import java.util.Scanner;
import java.util.Arrays;

public class array
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Asking the user for array size
        System.out.println("Enter the array size : ");
        int size = scan.nextInt();

        // Creating the array
        String arr[] = new String[size];

        System.out.println("Enter the array elements : ");
        // Takes the string as input from the user
        for(int i = 0;i<size;i++)
        {
            arr[i] = scan.next();
        }

        System.out.println("The array elements are : ");
        // Array elements printed using Stream API
        Arrays.stream(arr).forEach(System.out::println);  
    }
}

Output:

Enter the array size : 6
Enter the array elements : bus train flight car metro bike
The array elements are : 
bus
train
flight
car
metro
bike

Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Related Java Programs: