Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class act_3 {
- public static boolean isAlphabet(String input) {
- Pattern pattern = Pattern.compile("[\\d-!$%^&#*()_+|~=`{}\\[\\]:\";'<>?,.\\/]");
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- return false;
- }
- return true;
- }
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- // boolean flag to check if we are still repeating
- boolean repeat = true;
- while(repeat) {
- System.out.print("How many students: ");
- int numOfStudents = input.nextInt();
- // Array that will hold the values of each students
- String studentName[] = new String[numOfStudents];
- String studentNumber[] = new String[numOfStudents];
- String course[] = new String[numOfStudents];
- double midTermGrade[] = new double[numOfStudents];
- // this consumes the nextLine that nextInt doesn't, which fixes the new line spacing bug
- input.nextLine();
- for (int i = 0; i < numOfStudents; i++) {
- System.out.println("Student " + (i+1));
- // boolean flags again to check if the requirements are met
- boolean validName = false;
- boolean validCourse = false;
- boolean validGrade = false;
- while(!validName) {
- System.out.print("Enter Student Name: ");
- studentName[i] = input.nextLine();
- if (isAlphabet(studentName[i])) {
- validName = true;
- }
- else {
- System.out.println("Wrong input Alphabet characters only. Please try again");
- }
- }
- System.out.print("Enter Student #: ");
- studentNumber[i] = input.nextLine();
- while(!validCourse) {
- System.out.print("Enter Course: ");
- course[i] = input.nextLine();
- if (isAlphabet(course[i])) {
- validCourse = true;
- }
- else {
- System.out.println("Wrong input Alphabet characters only. Please try again");
- }
- }
- while(!validGrade) {
- System.out.print("Enter Mid Term Grade: ");
- midTermGrade[i] = input.nextDouble();
- if (midTermGrade[i] >= 50 && midTermGrade[i] <= 100) {
- validGrade = true;
- }
- else {
- System.out.println("Wrong input Please input 50-100 only. Please try again");
- }
- }
- input.nextLine();
- System.out.println();
- }
- // Loop for displaying all Students
- for (int i = 0; i < numOfStudents; i++) {
- System.out.println("Student " + (i+1));
- System.out.println("Student Name: " + studentName[i]);
- System.out.println("Student #: " + studentNumber[i]);
- System.out.println("Course: " + course[i]);
- System.out.println("Mid Term Grade: " + midTermGrade[i]);
- System.out.println();
- // Rating - to be added
- // Remarks - to be added
- }
- System.out.print("Do you like to try again Y/N: ");
- String askToRepeat = input.nextLine();
- if (askToRepeat.equalsIgnoreCase("Y") || askToRepeat.equalsIgnoreCase("Yes")) {
- repeat = true;
- }
- else if (askToRepeat.equalsIgnoreCase("N") || askToRepeat.equalsIgnoreCase("No")) {
- repeat = false;
- System.out.println("Program exiting now...");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement