Design a Java program that asks for the number of students registered in a course. The user should be prompted to enter the number of students enrolled in a course. If the number of students is greater than 0, use a counter-controlled while loop to prompt the user to enter the names of the students registered for the class. Create an output file that contains the names of the students in the class. Display a message to the user when the program is complete.
public class Main { public static void main(String[] args) { int number = 0; Scanner input = new Scanner(System.in); System.out.println("Enter number of students enrolled in course: "); try{ number = input.nextInt(); }catch (IllegalArgumentException e){ e.printStackTrace(); } if(number > 0){ try(PrintWriter out = new PrintWriter(new File("Students"))) { int i = 0; while (i < number) { System.out.print("Enter name of " + (i + 1) + " student "); out.print(input.next() + " "); i++; } }catch (IOException e){ System.out.println("I/O Error: " + e); } } System.out.println("Program is complete!"); } }
Comments
Leave a comment