Array

Program to read and print string of one dim array in Java

Program to read and print string of  one dim array in Java

In this tutorial, we will discuss the concept of Program to read and print string of  one dim array in Java

In this topic, we are going to learn how to read and print Strings of  one dimensional array in Java programming language  using 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)

String array

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 read and print String of one dim array

Read and print strings Using for loop

In this program, we are briefing input strings of an array and then print them using for loop in Java language

Program 1

//Java program to read and print elements a array using for
import java.util.Scanner;
class Array_Sin_Dim_String_For1{
public static void main (String args[]){
    Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len]; 
//declaration and creation of a string array

System.out.println("Enter the "+len+" String");
for(int i=0; i<len; i++){
arr[i]=scan.next(); 
//Reading the input from the user
}
System.out.println("\nEntered string are:");
for(int j=0; j<len; j++){
System.out.println(arr[j]); 
}//display every array element
}
}

When the above code is executed, it produces the following result

Output

 

Program 2

//Java program to read and print elements a array using for
import java.util.Scanner;
class Array_Sin_Dim_String_For2{
public static void main (String args[]){
    Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len]; 
//declaration and creation of an array

System.out.println("Enter the "+len+" String");
for(int i=0; i<len; i++){
    System.out.print("[arr["+i+"]:");
arr[i]=scan.next(); 
//Reading the input from the user
}
System.out.println("\nEntered string are:");
for(int j=0; j<len; j++){
System.out.println("[arr["+j+"]:"+arr[j]);
}//display every array element
}
}

When the above code is executed, it produces the following result

Output

 

Read and print strings Using while loop

In this program, we are briefing input strings of an array and then print them using while loop in Java language

Program 1

//Java program to read and print String an array using while
import java.util.Scanner;
class Array_Sin_Dim_String_While1{
public static void main (String args[]){
    Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len]; 
//declaration and creation of an array

System.out.println("Enter the "+len+" String");
int i=0;
while(i<len){
arr[i]=scan.next(); 
//Reading the input from the user
 i++;
}
System.out.println("\nEntered string are:");

int j=0; 
while(j<len){
System.out.println(arr[j]); 
j++;
}//display every array element
 
}
}

When the above code is executed, it produces the following result

Output

Program 2

//Java program to read and print String a array using while
import java.util.Scanner;
class Array_Sin_Dim_String_While2{
public static void main (String args[]){
    Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len]; 
//declaration and creation of an array

System.out.println("Enter the "+len+" String");
int i=0;
while(i<len){
    System.out.print("[arr["+i+"]:");
arr[i]=scan.next(); 
//Reading the input from the user
 i++;
}
System.out.println("\nEntered string are:");
int j=0; 
while(j<len){
System.out.println("[arr["+j+"]:"+arr[j]);
 j++;
}//display every array element
}
}

When the above code is executed, it produces the following result

Output

 

Read and print strings Using do-while loop

In this program, we are briefing input strings of an array and then print them using do-while loop in Java language

Program 1

//Java program to read and print String an array using while
import java.util.Scanner;
class Array_Sin_Dim_String_DoWhile1{
public static void main (String args[]){
    Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len]; 
//declaration and creation of an array

System.out.println("Enter the "+len+" String");
int i=0;
do{
arr[i]=scan.next(); 
//Reading the input from the user
 i++;
}while(i<len);
System.out.println("\nEntered string are:");

int j=0; 
do{
System.out.println(arr[j]); 
j++;
//display every array element
}while(j<len);

 
}
}

When the above code is executed, it produces the following result

Output

 

 

Program 2

//Java program to read and print String a array using do-while
import java.util.Scanner;
class Array_Sin_Dim_String_DoWhile_2{
public static void main (String args[]){
    Scanner scan=new Scanner(System.in);
//create a scanner instance for receives input
// from the user - input from keyboard
System.out.print("Enter the Array length: ");
//Ask input from the user for array length
int len=scan.nextInt();
//Reading the input from the user
String arr[]=new String[len]; 
//declaration and creation of an array

System.out.println("Enter the "+len+" String");
int i=0;
do{
    System.out.print("[arr["+i+"]:");
arr[i]=scan.next(); 
//Reding the input from the user
 i++;
}while(i<len);
System.out.println("\nEntered string are:");
int j=0; 
do{
System.out.println("[arr["+j+"]:"+arr[j]);
 j++;
}while(j<len);
//display every array element
}
}

When the above code is executed, it produces the following result

Output

 

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

Array in Programming language

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

C++ program to read and print elements of an one dim array
C++ code to Read and print string of one dim array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

6 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

6 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

6 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

9 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

1 year ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

1 year ago