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

Asked 11-Mar-2018
Viewed 797 times

1 Answer


1

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");
  }
 }
}