Answer to Question #58415 in Java | JSP | JSF for Lekorius Wallace

Question #58415
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.
1
Expert's answer
2016-03-18T15:48:04-0400
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS