String user input in an array
Table of Contents
In this article, we will discuss the concept of Java program to read user input(String) for an array
In this post, we are going to learn how to write a program to read string elements in single dimensional array using for, while and do-while loop in Java language
In this code, we are going to learn how to read string elements (user input) in single dimensional array using for loop in Java language
Program 1
import java.util.Scanner;
public class InputstringArrayInputfor{
public static void main(String args[]){
//scanner class to read input from the user
Scanner sc=new Scanner(System.in);
System.out.print("How many students on your class: ");
//user input the array length
int len=sc.nextInt();//Reading the given array length
//Declaring and creating array
String[] names=new String[len];
//initializing value to the array
System.out.println("\n******Initializing Strings of the array******");
System.out.println("Enter "+names.length+" names of your students");
for(int i=0; i<names.length; i++){
System.out.print("Enter name of student for index names["+i+"]:");
names[i]=sc.next();
//using the for loop to initializing array
}
//displaying the array
System.out.println("\n******displaying given student names******");
System.out.println("Entered student names are");
for(int i=0; i<names.length; i++){
//display entered strings using for loop
System.out.println(names[i]+"\t");
}
}
}
When the above code is executed, it produces the following result
How many students on your class: 4 ******Initializing Strings of the array****** Enter 4 names of your students Enter name of student for index names[0]:Sumi Enter name of student for index names[1]:Mani Enter name of student for index names[2]:Kani Enter name of student for index names[3]:Jeni ******displaying given student names****** Entered students names are: Sumi Mani Kani Jeni
In this code, we are going to learn how to read string elements (user input) in single dimensional array using while loop in Java language
Program 2
import java.util.Scanner;
public class StringArrayInputwhile{
public static void main(String args[]){
//scanner class to read input from the user
Scanner sc=new Scanner(System.in);
System.out.print("How many stuents on your class: ");
//user input for array length
int len=sc.nextInt();//Reading the input for array length
//Declaring and creating string array
String[] names=new String[len];
//initializing value to the array
System.out.println("\n******Initializing Strings of the array******");
System.out.println("Enter "+names.length+" names of your students");
int i=0;
while(i<names.length){
System.out.print("Enter name of student for index names["+i+"]:");
names[i]=sc.next();//reading names
//using the while loop to initializing array
i++;
}
//displaying the array
System.out.println("\n******displaying given student names******");
System.out.println("Entered student names are");
i=0;
while(i<names.length){
//display entered strings using while loop
System.out.println(names[i]+"\t");
i++;
}
}
}
When the above code is executed, it produces the following result
How many students on your class: 5 ******Initializing Strings of the array****** Enter 5 names of your students Enter name of student for index names[0]:Uma Enter name of student for index names[1]:Kuna Enter name of student for index names[2]:Hema Enter name of student for index names[3]:Sema Enter name of student for index names[4]:Puma ******displaying given student names****** Entered students names are: Uma Kuna Hema Sema Puma
In this code, we are going to learn how to read string elements (user input) in single dimensional array using do-while loop in Java language
Program 3
import java.util.Scanner;
public class StringArrayInputDowhile{
public static void main(String args[]){
//scanner class to read input from the user
Scanner sc=new Scanner(System.in);
System.out.print("How many stuents on your class: ");
//user input for array length
int len=sc.nextInt();//Reading the input for array length
//Declaring and creating string array
String[] names=new String[len];
//initializing value to the array
System.out.println("\n******Initializing Strings of the array******");
System.out.println("Enter "+names.length+" names of your students");
int i=0;
do{
System.out.print("Enter name of student for index names["+i+"]:");
names[i]=sc.next();//reading names
//using the do-while loop to initializing array
i++;
}while(i<names.length);
//displaying the array
System.out.println("\n******displaying given student names******");
System.out.println("Entered student names are");
i=0;
do{
//display entered strings using do-while loop
System.out.println(names[i]+"\t");
i++;
}while(i<names.length);
}
}
When the above code is executed, it produces the following result
How many students on your class: 3 ******Initializing Strings of the array****** Enter 3 names of your students Enter name of student for index names[0]:Balan Enter name of student for index names[1]:Malan Enter name of student for index names[2]:Selan ******displaying given student names****** Entered students names are: Balan Malan Selan
Suggested for you
Variable in C language
Data type in C language
One dimensional array in C language
Variable in C++ language
Data type in C++ language
One dimensional array in C++ language
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
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.