How to Read in a String to Array in Java

Take String Array input in Java language

Take String Array input in Coffee language

Contents

  • 1 Take String Array input in Java language
    • 1.1 Code to  fill cord array elements of array
      • 1.one.1 Program to fill up String in array using for loop
      • 1.1.2 Program to fill String array using while loop
      • 1.one.3 Program to make full String in an array using practise-while loop
    • ane.2 Related posts:

In this tutorial, nosotros will discuss the concept ofTake String Array input in Coffee language

In this topic, nosotros are going to learn how to take string assortment input in Coffee programming linguistic communicationusing for, while and exercise-while loops.

In this blog, We have already talk over that "What is an array", type of arrays and how to admission it(one dim, two dim and three dim arrays)

Arrays are the special type of variables to store Multiple type of values(int, cord,char, etc)under the same name in the continuous memory location. we can be hands accessed using the indices(indexes)

string elemnts - Take String Array input in Java language
Print String array elements

Here,

  • s1,s2,s3,s4 and s5 represent the string elements of the array
  • 0,1,two,3 and four represent the index of the array which is used to access the array elements
  • one,ii,3,4 and 5 represent the length of the array

Code to  fill string assortment elements of array

Program to fill Cord in array using for loop

In this program, nosotros are briefing how to accept input  Cord elements  of an assortment using for loop in Java language

Program i

import coffee.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 assortment     String[] arr=new String[4];          //Display default value after declaring     System.out.println("Default values of given String array: ");     for(int i=0; i<arr.length; i++){         System.out.println(arr[i]+"\t");     }//detect default value of the given assortment     System.out.println("");     //initializing value to the array     Organisation.out.println("******Initializing array******");     Organisation.out.println("Enter "+arr.length+" string values");     for(int i=0; i<arr.length; i++){         arr[i]=sc.nextLine();     }//using the for loop to initializing array          //displaying the array elements     System.out.println("\n******displaying assortment elements******");     System.out.println("Entered Strings are");     for(int i=0; i<arr.length; i++){         Organization.out.println(arr[i]+"\t");     } } }        

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

Default values of given String array: null zip zilch null  ******Initializing array****** Enter 4 String value Ani Abi Kavi Puvi  ******displaying array elements****** Entered Strings are Ani Abi Kavi Puvi

Program to fill Cord array using while loop

In this program, we are briefing how to input  String elements  of an array using while loop in Coffee language

Program 2

import java.util.Scanner; public class TakeStringArrayInputWhile{ public static void principal(String args[]){ //scanner form to read input from the user     Scanner sc=new Scanner(System.in);     //Declaring and creating Cord assortment     String[] arr=new Cord[4];          //Brandish default value later on declaring     System.out.println("Default values of given String array: ");     int i=0;      while(i<arr.length){         Arrangement.out.println(arr[i]+"\t");         i++;     }//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+" String values");     i=0;     while(i<arr.length){         arr[i]=sc.nextLine();          i++;     }//using the for loop to initializing array          //displaying the assortment elements     System.out.println("\north******displaying array elements******");     System.out.println("Entered array elements are");     i=0;     while(i<arr.length){         System.out.println(arr[i]+"\t");          i++;     } } }        

When the higher up code is executed, information technology produces the post-obit result

Default values of given String assortment: zero zilch null aught  ******Initializing array****** Enter 4 Cord value Krish Muru Saman Sonu  ******displaying array elements****** Entered Strings are Krish Muru Saman Sonu

Program to fill String in an assortment using do-while loop

In this plan, we are briefing how to input  String elements  of an array using do-while loop in Java linguistic communication

Program three

import java.util.Scanner; public form TakeStringArrayInputDoWhile{ public static void main(String args[]){ //scanner form to read input from the user     Scanner sc=new Scanner(System.in);     //Declaring and creating Cord array     String[] arr=new Cord[5];          //Brandish default value after declaring     Organization.out.println("Default values of given String array: ");     int i=0;      do{         Arrangement.out.println(arr[i]+"\t");         i++;     }while(i<arr.length);     //find default value of the given assortment           System.out.println("");     //initializing value to the assortment     System.out.println("******Initializing assortment******");     System.out.println("Enter "+arr.length+" String values");     i=0;     do{         arr[i]=sc.nextLine();          i++;     }while(i<arr.length);     //using the practise-while loop to initializing array          //displaying the assortment elements     Organisation.out.println("\n******displaying array elements******");     Organisation.out.println("Entered Strings are are");     i=0;     exercise{         Organisation.out.println(arr[i]+"\t");          i++;     }while(i<arr.length); } }        

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

Default values of given String array: null null null cypher null  ******Initializing array****** Enter 5 Cord value Sivaji Pirapu Kamal Rajani Ajith  ******displaying array elements****** Entered Strings are Sivaji Pirapu Kamal Rajani Ajith

Similar mail

Code to read and print integers of an assortment in Java

Lawmaking to read and print strings of an array in Java

Code to read and print characters of an array in Coffee

Code to read and impress integers of an array in C++

Lawmaking to read and print strings of an array in C++

Code to read and print characters of an array in C++

Lawmaking to read and impress integers of an assortment in C

Code to read and print strings of an array in C

Suggested f or you

Array in Programming language

Ane dimensional array in C language

One dimensional array in C++ language

Two dimension assortment in Java language

Two dimension array in C linguistic communication

Two dimension array in C++ language

Three dimension array in Coffee language

Three dimension assortment in C language

Three dimension array in C++ language

for loop in Java language

while loop in Java language

Exercise-while loop in Java language

Enhanced for loop in Java language

Information  type and variable in Coffee language

Operator in Coffee language

moultonuporn1938.blogspot.com

Source: https://code4coding.com/take-string-array-input-in-java-language/

0 Response to "How to Read in a String to Array in Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel