0

I am working on a simple program to ask user to enter student data with name, enrollment, CGPA, and Degree program.

Also, a menu that allows the user to add the student data or search for it by name of the student. The program consists of one class student.

class student {
    private
    String name, degreeProgram;
    int enroll;
    float cgpa;

    public void getData() {
        System.out.println("\t--------ENTER STUDENT DETAILS--------");
        System.out.print("\tEnter Name of the Student: ");
        Scanner getName = new Scanner(System.in);
        name = getName.nextLine();
        System.out.print("\tEnter Enrollment Number of the Student: ");
        Scanner getEnroll = new Scanner(System.in);
        enroll = getEnroll.nextInt();
        System.out.print("\tEnter CGPA of the Student: ");
        Scanner getcgpa = new Scanner(System.in);
        cgpa = getcgpa.nextFloat();
        System.out.print("\tEnter Degree Program of the Student: ");
        Scanner getDP = new Scanner(System.in);
        degreeProgram = getDP.nextLine();
    }

    public void showData() {
        System.out.print("\n\t--------DETAILS ARE AS FOLLOWS--------");
        System.out.print("\n\tNAME OF THE STUDENT IS: " + name);
        System.out.print("\n\tENROLLMENT OF THE STUDENT IS: " + enroll );
        System.out.print("\n\tCGPA OF THE STUDENT IS: " + cgpa);
        System.out.println("\n\tDEGREE PROGRAM OF THE STUDENT IS: " + degreeProgram);
    }
}

In the main Class Lab3 creates one object array that is s1. also created a menu for user to either add student data or search data.

public class Lab3 {
    public static void main(String[] args) {
        System.out.print("\nHow many Students Data you want to enter:");
        Scanner Mult = new Scanner(System.in);
        int entries = Mult.nextInt();
        student s1[] = new student[entries];    //CREATE student Object s1
        for (int i = 0; i < entries; i++) {
            s1[i] = new student();
            s1[i].getData();
        }
        System.out.println("***YOU WANT TO SEARCH OR ADD DATA***");
        System.out.print("\nEnter 1 for Adding Data\t Enter 2 for Searching Data: \n");
        Scanner choose = new Scanner(System.in);
        int choice = choose.nextInt();
        if (choice == 1) {
            System.out.print("\nHow many Students Data you want to enter:");
            Scanner multi = new Scanner(System.in);
            int entry = multi.nextInt();
            for (int i = 0; i < entry; i++) {
                s1[i] = new student();
                s1[i].getData();
            }
        } else if (choice == 2) {
            System.out.print("\nWhich Student data You want to Search Enter Name: ");
            Scanner std = new Scanner(System.in);
            String sh = std.nextLine();
            System.out.println();
            for (int i = 0; i < entries; i++) {
                if ("sh"=="sh")  {
                    s1[i].showData();
                } else{
                    System.out.println("Not Found");
                }
            }
        }

    }
}

The program works perfectly, user can add multiple entries or add more entries.

But the user search data by name then it shows all the data we entered. it could not display data only searched data. even I could not set if condition correctly. I tried my best to solve it but could not.

The problem is in the main Class Lab3 in else if condition for loop.

else if (choice == 2) {
            System.out.print("\nWhich Student data You want to Search Enter Name: ");
            Scanner std = new Scanner(System.in);
            String sh = std.nextLine();
            System.out.println();
            for (int i = 0; i < entries; i++) {
                if ("sh"=="sh")  {      //**Problem**
                    s1[i].showData();
                } else{
                    System.out.println("Not Found");
                }
           }

0 Answers0