Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Program9A {
  4. private static int maxNumberStudents = 0;
  5. private static int actualNumberStudents = 0;
  6. private static String[] studentNames;
  7.  
  8. public static void main(String[] args) {
  9. System.out.print("Please enter the maximum number of students: ");
  10. Scanner input = new Scanner(System.in);
  11. maxNumberStudents = input.nextInt(); //Gets input from the user.
  12. studentNames = new String[maxNumberStudents];
  13. enterStudentNames(studentNames);
  14. printStudentNames(studentNames);
  15. sortStudentNames(studentNames);
  16. }
  17.  
  18. public static void enterStudentNames(String[] studentNames) {
  19. Scanner nameInput = new Scanner(System.in);
  20.  
  21. for (actualNumberStudents = 0; actualNumberStudents < maxNumberStudents; actualNumberStudents++) {
  22. System.out.print("First name (enter period . to quit): ");
  23. String firstName = nameInput.next();
  24. if (firstName.equals(".")) {
  25. break;
  26. }
  27. System.out.print("Last name: ");
  28. String lastName = nameInput.next();
  29. String fullStudentName = lastName + ", " + firstName;
  30. studentNames[actualNumberStudents] = fullStudentName;
  31. }
  32. }
  33.  
  34. static void printStudentNames(String[] list) {
  35. for (int i = 0; i < actualNumberStudents; i++) {
  36. if (list[i].isEmpty() || list[i] == null) {
  37. break; //Stops the program if a null is read.
  38. }
  39. System.out.printf("%d. %s\n", i+1, list[i]);
  40.  
  41. }
  42. }
  43. public static void sortStudentNames(String[] studentNames) {
  44. int j;
  45. String temp;
  46. for (j = 0; j < actualNumberStudents; j++)
  47. {
  48. for (int i = j + 1; i < actualNumberStudents; i++) {
  49. if (studentNames[j].compareTo(studentNames[i]) > 0) {
  50. temp = studentNames[j];
  51. studentNames[j] = studentNames[i];
  52. studentNames[i] = temp;
  53. }
  54. }
  55. System.out.printf("\n%d. %s", j+1, studentNames[j]);
  56. }
  57. }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement