Code in JAVA For this option the user has to enter the stude
Code in JAVA:
For this option the user has to enter the student ID. The details of the student with the entered ID is displayed if it is found, otherwise a message is displayed to tell the user the student does not exist.
Solution
Please follow the code and comments for description :
CODE :
import java.util.*; // required imports
public class Student { // class that runs the code with the respective methods
    private final int sId; // instance variables for the class
     private final String sName;
     private final int sAge;
     private final String sCourse;
     private final String sYear;
     private final String sSection;
    public Student(int ID, String name, int age, String course, String year, String section) { // parameterised constructor
         sId = ID;
         sName = name;
         sAge = age;
         sCourse = course;
         sYear = year;
         sSection = section;
     }
    public int getsId() { // methods that gets the data from the constructor
         return sId;
     }
    public String getName() {
         return sName;
     }
    public int getAge() {
         return sAge;
     }
    public String getCourse() {
         return sCourse;
     }
    public String getYear() {
         return sYear;
     }
    public String getSection() {
         return sSection;
     }
    @Override
     public String toString() { // string method that returns the data required by the user
         return \"\ Student ID : \" + sId + \"\ Name : \" + sName + \"\ Age : \" + sAge + \"\ Course : \" + sCourse + \"\ Year : \" + sYear + \"\ Section : \" + sSection;
     }
    public static void main(String[] args) { // driver method
       
         ArrayList<Student> students = new ArrayList<>(); // arraylist that has the data stored
         Scanner input = new Scanner(System.in); // scanner class that gets the data from the user
        int menuChoice = 0; // required initialisations
       
         do { // iteratingover the options entered by the user
             System.out.println(\"\ \\t\\t\\tStudent Data\"); // prompt for the user about the data and the code
             System.out.println(\"\ 1. Add a Student.\ 2. Search For a Student.\ 3. Exit\");
             try {
                 System.out.println(\"\ Enter a choice : \"); // getting the choice
                 menuChoice = Integer.parseInt(input.nextLine());
             } catch (NumberFormatException e) { // catch the exception if any
                 continue;
             }
if (menuChoice == 1) { // if the option is 1
                 int id = -1; // get the full details for the student
                 do {
                     try {
                         System.out.println(\"Enter the Id : \");
                         id = Integer.parseInt(input.nextLine());
                     } catch (NumberFormatException e) {
                         System.out.println(\"Enter a number!\");
                     }
                 } while (id <= 0);
                 System.out.println(\"Enter the Full name : \"); // enter the name and the details
                 String name = input.nextLine();
                 int age = -1;
                 do {
                     try {
                         System.out.println(\"Enter the Age :\"); // enter the age
                         age = Integer.parseInt(input.nextLine());
                     } catch (NumberFormatException e) {
                         System.out.println(\"Enter a number!\");
                     }
                 } while (age <= 0);
                 System.out.println(\"Enter the Course : \"); // enter the course details
                 String course = input.nextLine();
                 System.out.println(\"Enter the Year : \");
                 String year = input.nextLine();
                 System.out.println(\"Enter the Section : \");
                 String section = input.nextLine();
                 Student student = new Student(id, name, age, course, year, section);
                 students.add(student);
             } else if (menuChoice == 2) { // if the choice is 2
               
                  boolean sFlag = false;
                 int id = -1;
                 do {
                     try {
                         System.out.println(\"Enter the Student Id to search : \"); // get the data to search
                         id = Integer.parseInt(input.nextLine()); // getting the data
                     } catch (NumberFormatException e) {
                         System.out.println(\"Enter a number!\");
                     }
                 } while (id <= 0);
                 for (Student student : students) { // iterating over the list
                     if (student.sId == id) { // check for the id
                         sFlag = true;
                         System.out.println(\"\ The Required Student Data is : \" + student); // print the data if found
                     } else {
                         sFlag = false;                      
                      }
                 }
               
                  if (sFlag == false) {
                     System.out.println(\"Sorry.! The Student Does Not Exist.\"); // message if the data is not found
                 }
             } else if(menuChoice == 3) { // if the choice is 3
                 System.out.println(\"Bye.!\"); // exit the code
             }
         } while (menuChoice < 3);
     }
 }
 OUTPUT :
            Student Data
1. Add a Student.
 2. Search For a Student.
 3. Exit
Enter a choice :
 1
 Enter the Id :
 1
 Enter the Full name :
 John
 Enter the Age :
 25
 Enter the Course :
 Msc
 Enter the Year :
 2015
 Enter the Section :
 A
Student Data
1. Add a Student.
 2. Search For a Student.
 3. Exit
Enter a choice :
 2
 Enter the Student Id to search :
 1
The Required Student Data is :
 Student ID : 1
 Name : John
 Age : 25
 Course : Msc
 Year : 2015
 Section : A
Student Data
1. Add a Student.
 2. Search For a Student.
 3. Exit
Enter a choice :
 2
 Enter the Student Id to search :
 2
 Sorry.! The Student Does Not Exist.
Student Data
1. Add a Student.
 2. Search For a Student.
 3. Exit
Enter a choice :
 3
 Bye.!
 Hope this is helpful.




