Advertisement
Guest User

Java Act 3

a guest
Dec 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.04 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class act_3 {
  6.     public static boolean isAlphabet(String input) {
  7.         Pattern pattern = Pattern.compile("[\\d-!$%^&#*()_+|~=`{}\\[\\]:\";'<>?,.\\/]");
  8.         Matcher matcher = pattern.matcher(input);
  9.         if (matcher.find()) {
  10.             return false;
  11.         }
  12.         return true;
  13.     }
  14.     public static void main(String[] args) {
  15.         Scanner input = new Scanner(System.in);
  16.         // boolean flag to check if we are still repeating
  17.         boolean repeat = true;
  18.  
  19.         while(repeat) {
  20.             System.out.print("How many students: ");
  21.             int numOfStudents = input.nextInt();
  22.  
  23.             // Array that will hold the values of each students
  24.             String studentName[] = new String[numOfStudents];
  25.             String studentNumber[] = new String[numOfStudents];
  26.             String course[] = new String[numOfStudents];
  27.             double midTermGrade[] = new double[numOfStudents];
  28.  
  29.             // this consumes the nextLine that nextInt doesn't, which fixes the new line spacing bug
  30.             input.nextLine();
  31.  
  32.             for (int i = 0; i < numOfStudents; i++) {
  33.                 System.out.println("Student " + (i+1));
  34.                 // boolean flags again to check if the requirements are met
  35.                 boolean validName = false;
  36.                 boolean validCourse = false;
  37.                 boolean validGrade = false;
  38.  
  39.                 while(!validName) {
  40.                     System.out.print("Enter Student Name: ");
  41.                     studentName[i] = input.nextLine();
  42.                     if (isAlphabet(studentName[i])) {
  43.                         validName = true;
  44.                     }
  45.                     else {
  46.                         System.out.println("Wrong input Alphabet characters only. Please try again");
  47.                     }
  48.                 }
  49.                 System.out.print("Enter Student #: ");
  50.                 studentNumber[i] = input.nextLine();
  51.                 while(!validCourse) {
  52.                     System.out.print("Enter Course: ");
  53.                     course[i] = input.nextLine();
  54.                     if (isAlphabet(course[i])) {
  55.                         validCourse = true;
  56.                     }
  57.                     else {
  58.                         System.out.println("Wrong input Alphabet characters only. Please try again");
  59.                     }
  60.                 }
  61.                 while(!validGrade) {
  62.                     System.out.print("Enter Mid Term Grade: ");
  63.                     midTermGrade[i] = input.nextDouble();
  64.                     if (midTermGrade[i] >= 50 && midTermGrade[i] <= 100) {
  65.                         validGrade = true;
  66.                     }
  67.                     else {
  68.                         System.out.println("Wrong input Please input 50-100 only. Please try again");
  69.                     }
  70.                 }
  71.                 input.nextLine();
  72.                 System.out.println();
  73.             }
  74.  
  75.             // Loop for displaying all Students
  76.             for (int i = 0; i < numOfStudents; i++) {
  77.                 System.out.println("Student " + (i+1));
  78.                 System.out.println("Student Name: " + studentName[i]);
  79.                 System.out.println("Student #: " + studentNumber[i]);
  80.                 System.out.println("Course: " + course[i]);
  81.                 System.out.println("Mid Term Grade: " + midTermGrade[i]);
  82.                 System.out.println();
  83.                 // Rating - to be added
  84.                 // Remarks - to be added
  85.             }
  86.  
  87.             System.out.print("Do you like to try again Y/N: ");
  88.             String askToRepeat = input.nextLine();
  89.             if (askToRepeat.equalsIgnoreCase("Y") || askToRepeat.equalsIgnoreCase("Yes")) {
  90.                 repeat = true;
  91.             }
  92.             else if (askToRepeat.equalsIgnoreCase("N") || askToRepeat.equalsIgnoreCase("No")) {
  93.                 repeat = false;
  94.                 System.out.println("Program exiting now...");
  95.             }
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement