Monday, January 26, 2015

Uses of array in java

The java program given below will store integer type data into an array and later the data will be displayed. First of all I have declared an integer type array month by the line int month[] = new int[30];

Later I have created a scanner object to get input from keyboard using the line Scanner in = new Scanner(System.in);

By the 12th line System.out.print("Enter the limit: "); a message will be displayed to enter a limit. There in the array we can store 31 integer type data in total, but by providing the limit we can control it. If we provide the limit 5, then only 5 integer type data will be stored and displayed.
array, integer type array, array in java
By the 13th line provided limit will be stored into the integer type variable j. Later there you will found two for loops, first one is to store the data and the second one is to display them.
import java.util.*;

public class ArrayTest{
 public static void main(String [] args){
  //declaring integer type array
  int month[] = new int[30];
  
  //creating a scanner object
  Scanner in = new Scanner(System.in);
  
  //getting input from keyboard
  System.out.print("Enter the limit: ");
  int j = in.nextInt();
  
  //for loop to get input into the array
  for (int i=0; i<=j;i++){
   month[i]=in.nextInt();
  }
  
  //for loop for displaying data from array
  for(int i=0; i<=j;i++){
   System.out.print(month[i]+", ");
  }
 }
}


No comments:

Post a Comment