Tuesday, January 27, 2015

Array to store string type data and displaying them using for loop

The java program given below will store several string type data into an array and later the stored data will be displayed using a for loop. When you will run this program then you will be asked to provide a limit (if you provide the limit 3, then you will be able to store 3 string type data). After providing the limit, you will be asked to store the string type data (for example: your friends name, name of several programming languages etc.).

After providing the data, soon they will be displayed.
string type array, array in java, string type array in java
import java.util.*;

public class CharArray{
 public static void main(String [] args){
  //creating scanner object
  Scanner in = new Scanner(System.in);
  
  //declaring string type array
  String friends[] = new String[50];
  
  //message to enter the limit
  System.out.print("Enter the limit: ");
  
  //storing the limit into the variable j
  int j = in.nextInt();

  //for loop to store data into the array
  for(int i=0; i<=j; i++){
   friends[i] = in.nextLine();
  }

  //for loop to display the stored data
  for(int i=0; i<=j; i++){
   System.out.print(friends[i]+"\n");
  }
 }
}


No comments:

Post a Comment