B Basak B Basak - 2 months ago 6
Java Question

Why is the value of the subclass data member not displayed?

I have created 2 java files: XCompanyShortlist.java and StudentDemo.java. The XCompanyShortlist.java contains the main method and all the user input like Student Registration No., Name, Semester, GPA, CGPA, Branch Name, Placement status and Internship status.

The StudentDemo.java has a superclass StudentDemo which initializes Reg. No., Name, Semester, GPA, CGPA using parameterized constructor and it also contains a method display() which displays all there informations.

A class BranchStudent extends StudentDemo class and contains an extra String named BranchName. This class also contains a display() method which calls the display() method in the superclass and also prints the BranchName. Another class StudentPlacement contains variables for InternshipStatus, PlacementStatus, and an array of preferred company list.

Here is the StudentDemo.java file code:

class StudentDemo {
long RegNo;
String fname;
short sem;
float gpa;
float cgpa;

StudentDemo() {
RegNo = 0;
fname = "";
sem = 0;
gpa = (float) 0.0;
cgpa = (float)0.0;
}
StudentDemo(long RegNo, String fname, short sem, float gpa, float cgpa) {
this.RegNo = RegNo;
this.fname = fname;
this.sem = sem;
this.gpa = gpa;
this.cgpa = cgpa;
}
StudentDemo(StudentDemo obj) {
RegNo = obj.RegNo;
fname = obj.fname;
sem = obj.sem;
gpa = obj.gpa;
cgpa = obj.cgpa;
}
void display() {
System.out.println("------------------------------------------");
System.out.println("Registration No. :"+RegNo);
System.out.println("Full Name: "+fname);
System.out.println("Semester: "+sem);
System.out.println("GPA: "+gpa);
System.out.println("CGPA: "+cgpa);
System.out.println("------------------------------------------");
}
}
class BranchStudent extends StudentDemo {
public String BranchName;
BranchStudent(long RegNo,String fname,short sem,float gpa,float cgpa,String BranchName) {
super(RegNo,fname,sem,gpa,cgpa);
this.BranchName = BranchName;
}
BranchStudent() {
super();
BranchName = "CSE";
}
BranchStudent(BranchStudent obj) {
super(obj);
BranchName = obj.BranchName;
}

void display() {
super.display();
System.out.println("Student Branch: "+BranchName);
}
}
class StudentPlacement extends BranchStudent {
String compList[];
int StatusPlacement, StatusIntern;

StudentPlacement() {
super();
StatusPlacement = 0;
StatusIntern = 0;
compList = new String[3];
}

StudentPlacement(StudentPlacement obj) {
super(obj);
StatusPlacement = obj.StatusPlacement;
StatusIntern = obj.StatusIntern;
compList = obj.compList;
}

StudentPlacement(long RegNo, String fname, short sem, float gpa, float cgpa, String BranchName,String compList[], int StatusPlacement,int StatusIntern) {
super(RegNo, fname, sem, gpa, cgpa, BranchName);
this.compList = compList;
this.StatusPlacement = StatusPlacement;
this.StatusIntern = StatusIntern;
}
}


Here is the XCompanyShortlist.java file code:

import java.util.Scanner;
public class XCompanyShortlist {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please Enter The Number Of Students: ");
int n = sc.nextInt();

StudentPlacement obj[] = new StudentPlacement[n];
for(int i = 0; i < n; i++) {
obj[i] = new StudentPlacement();
}
System.out.println("Please Enter The Student Details: ");
for(int i = 0; i < n; i++) {
System.out.print("Please Enter The Reg. No. :");
long RegNo = sc.nextLong();
sc.nextLine();
System.out.print("Please Enter The Full Name: ");
String fname = sc.nextLine();
System.out.print("Please Enter The Semester: ");
short sem = sc.nextShort();
System.out.print("Please Enter The GPA: ");
float gpa = sc.nextFloat();
System.out.print("Please Enter The CGPA: ");
float cgpa = sc.nextFloat();
System.out.print("Please Enter Branch Name:");
String branchName = sc.nextLine();
sc.nextLine();
System.out.println("Please Enter 3 Preferred Choice: ");
String compList[] = new String[3];
for(int x = 0; x < 3; x++) {
compList[x] = sc.nextLine();
}
System.out.print("Please Enter The Status Of Placement(0/1): ");
int statusPlacement = sc.nextInt();
System.out.print("Please Enter Status Of Internship(0/1): ");
int statusIntern = sc.nextInt();
obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern);
System.out.println();
}
for(int i = 0; i < n; i++) {
obj[i].display();
}
sc.close();
}

}


The problem I am facing is that all the student details from the StudentDemo superclass is being dislayed but the subclass BranchStudent is not printing the BranchName. I am unable to find the problem in my code.

OUTPUT:

Please Enter The Number Of Students:
1
Please Enter The Student Details:
Please Enter The Reg. No. :159101046
Please Enter The Full Name: Bitan Basak
Please Enter The Semester: 3
Please Enter The GPA: 8.86
Please Enter The CGPA: 8.64
Please Enter Branch Name:CSE
Please Enter 3 Preferred Choice:
HP
Dell
Microsoft
Please Enter The Status Of Placement(0/1): 0
Please Enter Status Of Internship(0/1): 0

------------------------------------------
Registration No. :159101046
Full Name: Bitan Basak
Semester: 3
GPA: 8.86
CGPA: 8.64
------------------------------------------
Student Branch:


This is the output given by my program. As you can see the Student Branch is not being printed. I am unable to understand why.

Answer

From what I can tell the issue has nothing to do with inheritance but rather that you are feeding an empty line to the constructor.

This means something is wrong with the usage of the Scanner.nextLine() method. If I change your code to this:

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Please Enter The Number Of Students: ");
        int n = sc.nextInt();

        StudentPlacement obj[] = new StudentPlacement[n];
        for(int i = 0; i < n; i++) {
            obj[i] = new StudentPlacement();
        }
        System.out.println("Please Enter The Student Details: ");
        for(int i = 0; i < n; i++) {
            System.out.print("Please Enter The Reg. No. :");
            long RegNo = sc.nextLong();
            sc.nextLine();
            System.out.print("Please Enter The Full Name: ");
            String fname = sc.nextLine();
            System.out.print("Please Enter The Semester: ");
            short sem = sc.nextShort();
            System.out.print("Please Enter The GPA: ");
            float gpa = sc.nextFloat();
            System.out.print("Please Enter The CGPA: ");
            float cgpa = sc.nextFloat();
            sc.nextLine();
            System.out.print("Please Enter Branch Name:");
            String branchName = sc.nextLine();
            System.out.println("Please Enter 3 Preferred Choice: ");
            String compList[] = new String[3];
            for(int x = 0; x < 3; x++) {
                compList[x] = sc.nextLine();
            }
            System.out.print("Please Enter The Status Of Placement(0/1): ");
            int statusPlacement = sc.nextInt();
            System.out.print("Please Enter Status Of Internship(0/1): ");
            int statusIntern = sc.nextInt();
            obj[i] = new StudentPlacement(RegNo,fname,sem,gpa,cgpa,branchName,compList,statusPlacement,statusIntern);
            System.out.println();
        }
        for(int i = 0; i < n; i++) {
            obj[i].display();
        }
        sc.close();
    }

I.e. move the sc.nextLine() before the Branch Name input the scanner picks up the correct value from the console.

Hope that helps.

Greetings