Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Program9A {
- private static int maxNumberStudents = 0;
- private static int actualNumberStudents = 0;
- private static String[] studentNames;
- public static void main(String[] args) {
- System.out.print("Please enter the maximum number of students: ");
- Scanner input = new Scanner(System.in);
- maxNumberStudents = input.nextInt(); //Gets input from the user.
- studentNames = new String[maxNumberStudents];
- enterStudentNames(studentNames);
- printStudentNames(studentNames);
- sortStudentNames(studentNames);
- }
- public static void enterStudentNames(String[] studentNames) {
- Scanner nameInput = new Scanner(System.in);
- for (actualNumberStudents = 0; actualNumberStudents < maxNumberStudents; actualNumberStudents++) {
- System.out.print("First name (enter period . to quit): ");
- String firstName = nameInput.next();
- if (firstName.equals(".")) {
- break;
- }
- System.out.print("Last name: ");
- String lastName = nameInput.next();
- String fullStudentName = lastName + ", " + firstName;
- studentNames[actualNumberStudents] = fullStudentName;
- }
- }
- static void printStudentNames(String[] list) {
- for (int i = 0; i < actualNumberStudents; i++) {
- if (list[i].isEmpty() || list[i] == null) {
- break; //Stops the program if a null is read.
- }
- System.out.printf("%d. %s\n", i+1, list[i]);
- }
- }
- public static void sortStudentNames(String[] studentNames) {
- int j;
- String temp;
- for (j = 0; j < actualNumberStudents; j++)
- {
- for (int i = j + 1; i < actualNumberStudents; i++) {
- if (studentNames[j].compareTo(studentNames[i]) > 0) {
- temp = studentNames[j];
- studentNames[j] = studentNames[i];
- studentNames[i] = temp;
- }
- }
- System.out.printf("\n%d. %s", j+1, studentNames[j]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement