How do I use a loop in ArrayList to give input in java?

1 votes
176 views
simron shukla asked 11-Mar-2018 in programming by simron shukla
How do I use a loop in ArrayList to give input in java?


1 Answer

1 votes
Arti Mishra answered 02-Apr-2018 by Arti Mishra
This program takes input as, number of student in the array and display one by one from array.
For Example:
 
import java.util.Scanner;
public class students {
  public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
  System.out.println("Enter Number of Students : ");
  int numOfStudents = Integer.parseInt(sc.nextLine());
  //Create a string array to store the names of students
  String arrayOfNames[] = new String[numOfStudents];
  for (int i = 0; i < arrayOfNames.length; i++) {
   System.out.println("Enter the name of student " + (i+1) + " : ");
          arrayOfNames[i] = sc.nextLine();
  }
  // show student name one by one
  for (int i = 0; i < arrayOfNames.length; i++) {
   System.out.print("Student " + (i+1) + " : ");
          System.out.print(arrayOfNames[i] + "\n");
  }
 }
}