Main method error can't figure out how to fix [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Getting a main method error which is only affecting my retrieve salary method I'm not sure what to do to fix the issue I have added particular items but I can't get the error to go away. Is the only thing on my program that brings up an error and I am unable to continue. Image included of the only issues I'm encountering now
"Error: Main method not found in class, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application"
Image included
public class finalproject {
public static class employeeCase {
EMPLOYEE[] employees;
int AMOUNT;
employeeCase(){
employees = new EMPLOYEE[100];
AMOUNT = 0;
}
private void loadEmployee() {
String ID = null;
int SALARY = 0;
Scanner sc = new Scanner(System.in);
System.out.println(" ");
System.out.println("How many employees do you want to load?: ");
int num = sc.nextInt();
for(int i = 0; i < num; i++){
// Display parallel arrays
System.out.println(" ");
System.out.println("Name: " + employees[i] + " " + "ID: " + ID + " " + "Salary: " + SALARY);
sc.close();
}
}
private int addEmployee() {
Scanner sc = new Scanner(System.in);
System.out.println(" ");
System.out.println("How many employees do you want to enter?: ");
AMOUNT = 0;
AMOUNT = sc.nextInt();
String Again1 = "no";
String Fname;
String ID;
int SALARY = 0;
do {
for(int i = 0; i < AMOUNT; i++) {
System.out.printf("Enter employee's first name: ");
Fname = sc.nextLine();
System.out.printf("Enter employee ID (5 digits): ");
ID = sc.next();
System.out.printf("Enter employee salary: ");
SALARY = sc.nextInt();
System.out.println(" ");
this.employees[this.AMOUNT] = new EMPLOYEE(Fname, ID, SALARY);
this.AMOUNT++;
sc.close();
} } while (Again1.equalsIgnoreCase("yes"));
return SALARY;
}
private void displayEmployee() {
for(int i = 0; i < AMOUNT; i++){
// Display parallel arrays
System.out.println(" ");
System.out.println(this.employees[i]);
}
}
private void retrieveSpecific() {
Scanner sc = new Scanner(System.in);
System.out.println(" ");
System.out.println("Enter employee ID: ");
String id = sc.next();
//Search for ID in all the stored employees
for(int i=0; i<this.AMOUNT; i++) {
if(id.contentEquals(this.employees[i].ID)) {
System.out.println(this.employees[i]);
sc.close();
}}}
private void retrieveSalary() {
Scanner scan = new Scanner(System.in);
System.out.println(" ");
System.out.println("Enter lowest employee salary: ");
int LSALARY = scan.nextInt();
System.out.println(" ");
System.out.println("Enter highest employee salary: ");
int HSALARY = scan.nextInt();
for(int i = 0; i < AMOUNT; i++) {
if(employees[i].SALARY >= LSALARY & employees[i].SALARY <= HSALARY) {
System.out.println(employees[i]);
scan.close();}}
}
public static void main(String[] args) {
employeeCase EmployeeData = new employeeCase();
Scanner sc = new Scanner(System.in);
int Select = 0;
do {
displayMenu();
System.out.print("Input your selection from the menu: ");
Select = sc.nextInt();
switch (Select) {
case 1 : EmployeeData.loadEmployee();
break;
case 2 : EmployeeData.addEmployee();
break;
case 3 : EmployeeData.displayEmployee();
break;
case 4 : EmployeeData.retrieveSpecific();
break;
case 5 : EmployeeData.retrieveSalary();
break;
case 6 : System.out.println("Thank you, goodbye!");
break;
default : System.err.println("Invalid Input");
break;
}
} while (Select != 6);
sc.close();
}
public static void displayMenu() {
System.out.println(" MENU");
System.out.println("============================================");
System.out.println("1: Load employees' data");
System.out.println("2: Add new employee");
System.out.println("3: Display all employees");
System.out.println("4: Retrieve specfic employee data");
System.out.println("5: Retrieve employee based on salary range");
System.out.println("6: Exit program");
}
}}

Your main method needs to be static. Change public void main to public static void main.

Access modifier for main method should be static here you define a non static main method
Replace your main method with
public static void main(String[] args){}

Related

Displaying Info

In a project for school, I'm required to make a menu system with the following options:
Load employees’ data - prompts user for the number of employees to be loaded and then prompts for each employee name, id (5 digit number), and annual salary
Add new employee - prompts user for an employee data, name, id, and annual salary
Display all employees - displays each employee’s data to the console, one employee per line
Retrieve specific employee’s data - prompts user for the employee id and displays the corresponding employee’s data: id, name, and salary
Retrieve employees with salaries based on range - prompts user for the lowest and highest salary and displays all employees with salaries in that range. Display each employee on separate line with all information - name, id, and salary
Exit
Each menu choice must be it's own be its own method, and he prefers we use arrays instead of lists, which is why I allotted 100 spaces. Here is what I have so far. Menu options 1, 2, and 6 run without breaking the program. But anything where things have to be displayed seems to break. My best guess is that there is an issue with passing and updating the array "employees" between all of the methods.
package practice;
import java.util.Arrays;
import java.util.Scanner;
public class Project {
public static Employee[] loadData() {
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
System.out.println("How many employees would you like to add?"); //Determines how many employees to create
Employee[] employees = new Employee[100];
int numberEmployees = scanint.nextInt();
for (int i = 0; i < numberEmployees; i++) {
System.out.println("Enter name: ");
String empName = scanstr.nextLine();
System.out.println("Enter ID: ");
int empID = scanint.nextInt();
System.out.println("Enter Salary: ");
int empSalary = scanint.nextInt();
employees[i+1] = new Employee(empName, empID, empSalary); //Add employee to array
}
return employees;
}
public static Employee addEmployee() {
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
System.out.println("Enter name: ");
String empName = scanstr.nextLine();
System.out.println("Enter ID: ");
int empID = scanint.nextInt();
System.out.println("Enter salary: ");
int empSalary = scanint.nextInt();
return new Employee(empName, empID, empSalary);
}
public static void displayEmployees(Employee[] employees) {
for (int i = 0; i < employees.length; i++) {
if (employees[i] != null) {
System.out.println(Arrays.toString(employees));
//System.out.println("Employee Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
}
}
public static void specificEmployee(Employee[] employees, int id) {
for (int i = 0; i < employees.length; i++) {
if (employees[i].id == id) {
System.out.println("Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
else {
System.out.println("ID not recognized");
}
}
}
public static void salaryRange(Employee[] employees, int salaryMinimum, int salaryMaximum) {
for (int i = 0; i < employees.length; i++) {
if (employees[i].salary >= salaryMinimum && employees[i].salary <= salaryMaximum) {
System.out.println("Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
else {
System.out.println("No employees within salary range");
}
}
}
public static void main(String[] args) {
Scanner scanint = new Scanner(System.in);
Scanner scanstr = new Scanner(System.in);
int menuChoice = 0;
while (menuChoice != 6) {
System.out.println("\tMenu:");
System.out.println("1. Load Employee Data");
System.out.println("2. Add New Employee");
System.out.println("3. Display All Employees");
System.out.println("4. Retrieve Specific Employee Data");
System.out.println("5. Retrieve Employees Within Salary Range");
System.out.println("6. Exit");
menuChoice = scanint.nextInt();
Employee[] employees = new Employee[100];
int amountEmployees;
if (menuChoice == 1) {
employees = loadData();
}
else if (menuChoice == 2) {
employees[0] = addEmployee();
}
else if (menuChoice == 3) {
displayEmployees(employees);
}
else if (menuChoice == 4) {
System.out.println("Enter 5 digit employee ID: ");
int id = scanint.nextInt();
specificEmployee(employees, id);
}
else if (menuChoice == 5) {
System.out.println("Enter minimum of salary range: ");
int salaryMinimum = scanint.nextInt();
System.out.println("Enter maximum of salary range");
int salaryMaximum = scanint.nextInt();
salaryRange(employees, salaryMinimum, salaryMaximum);
}
else if (menuChoice == 6) {
break;
}
else {
System.out.println("Invalid Choice");
}
}
scanint.close();
scanstr.close();
}
Any help would be appreciated!!!!!
There are a few problems in your code:
while (menuChoice != 6) {
System.out.println("\tMenu:");
System.out.println("1. Load Employee Data");
System.out.println("2. Add New Employee");
System.out.println("3. Display All Employees");
System.out.println("4. Retrieve Specific Employee Data");
System.out.println("5. Retrieve Employees Within Salary Range");
System.out.println("6. Exit");
menuChoice = scanint.nextInt();
**Employee[] employees = new Employee[100];**
int amountEmployees;
if (menuChoice == 1) {
employees = loadData();
}
You have declared the employees array inside the while loop, so each time you read an input you are creating a new one. This totally defeats the purpose of storing employees in an array and later retrieving them, which also makes the assignment redundant if menuChoice == 1. You must think about where you should declare your array.
public static void specificEmployee(Employee[] employees, int id) {
for (int i = 0; i < employees.length; i++) {
if (employees[i].id == id) {
System.out.println("Name: " + employees[i].name + " ID: " + employees[i].id + " Salary: " + employees[i].salary);
}
else {
System.out.println("ID not recognized");
}
}
}
When you declare an array, the values by default are null, unless you assign them some values. Here, you are iterating the entire array and checking for employee id. You should consider what would happen if the employee at a particular index in the array is null. You must have checks for such cases.
A similar problem is in one of the other methods that you have created. I will let you debug that on your own.
And as suggested by #NomadMaker - Your second menu item only sets element 0 of the array. You can't add two or more employees.
I would suggest, you try to dry run your code and if you find it tough, try debugging it in an ide.

Why won't my array pass to the other class?

I have looked at similar examples or other programs that call array from another class and it seems like I have done it the correct way but I am still getting errors.
This is where the arrarys are stored:
import java.util.Scanner;
public class DriverProgram {
public static int[] IDs = new int[10];
public static String[] names = new String[10];
public static double[] grades = new double[10];
public static int i = 0;
static Student call = new Student();
public static void main(String[] args){
call = new Student();
Scanner command = new Scanner(System.in);
System.out.println("Please Enter a command(i, r, s, or d): ");
while(command.hasNext()){
char command1 = command.next().charAt(0);
if(command1 == 'i'){
call.AddToArray(IDs[], names[] , grades[], i);
}else if(command1 == 'r'){
call.RemoveFromArray(int [] IDs, String [] names,double [] grades, int i);
}else if(command1 == 's'){
call.SortArray(int [] IDs, String [] names,double [] grades, int i);
}else if(command1 == 'd'){
call.DisplayArray(int [] IDs, String [] names,double [] grades, int i);
}else if(command1 == 'z') {
break;
}
else System.out.println("Invalid command enter a valid command next time.");
System.out.println("Please Enter a command(i, r, s, or d) or z to finish: ");
}
}
And this is what I am tryign to call the arrays to:
import java.util.Scanner;
public class Student {
public static void AddToArray(int[] IDs, String[] names, double[] grades, int i) {
if (i >= 10) {
System.out.println("You have already inputted 10 students please delete one first.");
} else {
Scanner readin = new Scanner(System.in);
Scanner readinname = new Scanner(System.in);
Scanner readingrade = new Scanner(System.in);
System.out.println("Please enter student ID: ");
IDs[i] = readin.nextInt();
System.out.println("Please enter student name: ");
names[i] = readinname.nextLine();
System.out.println("Please enter student grade: ");
grades[i] = readingrade.nextDouble();
System.out.println(IDs[i] + " " + names[i] + " " + grades[i]);
i++;
for (int j = 0; j < i; j++) {
if (IDs[j] == IDs[i]) {
System.out.println("This student has already been entered.");
}else{
System.out.println("The student has been added");
break;
}
}
}
}
I am not sure what else I need or what I am missing in order to call those arrays.
call.AddToArray(IDs[], names[] , grades[], i);
should be replaced with
call.AddToArray(IDs, names , grades, i);
P.S. Design notes
Student has only static method, so this is utilitly class and should not allowed an instance creation
call.AddToArray() and others static methods should be called as Student.AddToArray()
array is not correct data strucutre where you can add or remove elements. There're more suitable data structures like List or Map.
It's better to use only one instance of Scanner.
This is how you DriverProgram could look like.
public class DriverProgram {
public static void main(String[] args) {
Map<Integer, Student> students = new HashMap<>();
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
System.out.println("Please Enter a command [1-5]:");
System.out.println("1. add new student");
System.out.println("2. remove existed student");
System.out.println("3. sort existed students by grades desc");
System.out.println("4. show existed students");
System.out.println("5. exit");
System.out.print("> ");
int menu = scan.nextInt();
if (menu == 1)
addStudent(scan, students);
else if (menu == 2)
removeStudent(scan, students);
else if (menu == 3)
sortStudents(students);
else if (menu == 4)
showStudents(students);
else if (menu == 5)
break;
System.err.println("Unknown command. Try again");
}
}
private static void addStudent(Scanner scan, Map<Integer, Student> students) {
if (students.size() == 10) {
System.err.println("You have already inputted 10 students please delete one first.");
return;
}
System.out.print("Please enter student ID: ");
int id = scan.nextInt();
if (students.containsKey(id)) {
System.err.println("This student with this id has already been entered.");
return;
}
System.out.print("Please enter student name: ");
String name = scan.nextLine();
System.out.print("Please enter student grade: ");
double grade = scan.nextDouble();
students.put(id, new Student(id, name, grade));
}
private static void removeStudent(Scanner scan, Map<Integer, Student> students) {
}
private static void sortStudents(Map<Integer, Student> students) {
}
private static void showStudents(Map<Integer, Student> students) {
}
public static final class Student {
private final int id;
private final String name;
private final double grade;
public Student(int id, String name, double grade) {
this.id = id;
this.name = name;
this.grade = grade;
}
}
}

How do i use user input for a string array that is in a while loop?

I'm trying to convert the user input from the question of students name and score into a array.
I also need help to printout the array.
The while loop is using boolean loopNaming as its condition, and i is updated everytime the loop occurs.
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
double score;
boolean loopNaming=true;
int i=0;
String[] name = new String[i];
while(loopNaming==true)
{
System.out.printf("Enter name of student or done to finish: ");
name[i] = keyboard.next();
if(name[i].equals("done"))
{
loopNaming = false;
}
else
{
System.out.println("Enter score: ");
score = keyboard.nextDouble();
}
i=i+1;
}
System.out.println(name[i]);
}
}
You can simplify the logic of your program and write something like this,
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
List<String> nameList = new ArrayList<String>();
List<Double> scoreList = new ArrayList<Double>();
while (true) {
System.out.printf("Enter first name of student or done to finish: ");
String fname = keyboard.next();
if (fname.equals("done")) {
break;
}
System.out.printf("Enter last name of student: ");
String lname = keyboard.next();
nameList.add(fname + " " + lname);
System.out.println("Enter score: ");
scoreList.add(keyboard.nextDouble());
}
keyboard.close();
System.out.println("Names: " + nameList);
System.out.println("scores: " + scoreList);
}
I have changed the array to an arraylist and moved i=i+1; to inside else segment. Also changed the final print statement to print the list.
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
double score;
boolean loopNaming=true;
int i=0;
ArrayList<String> name = new ArrayList<String>();
while(loopNaming==true)
{
System.out.printf("Enter name of student or done to finish: ");
name.add(keyboard.next());
if(name.get(i).equals("done"))
{
loopNaming = false;
}
else
{
System.out.println("Enter score: ");
score = keyboard.nextDouble();
i=i+1;
}
}
System.out.println(name);
}
I would firstly recommend using a List data structure:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double score;
boolean loopNaming = true;
List<String> name = new ArrayList<>();
while (loopNaming) {
System.out.printf("Enter name of student or done to finish: ");
String input = keyboard.next();
if (input.equals("done")) {
loopNaming = false;
} else {
name.add(input);
System.out.println("Enter score: ");
score = keyboard.nextDouble();
}
}
System.out.println(name.toString());
}
However, if you very much would like to use an array, you could make your own method to increase the size of your array each time a new name is added:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double score;
int i = 0;
boolean loopNaming = true;
String[] name = {};
while (loopNaming) {
System.out.printf("Enter name of student or done to finish: ");
String input = keyboard.next();
if (input.equals("done")) {
loopNaming = false;
} else {
name = increaseArray(name);
name[i] = input;
System.out.println("Enter score: ");
score = keyboard.nextDouble();
i++;
}
}
System.out.println(Arrays.toString(name));
}
public static String[] increaseArray(String[] arr) {
String[] temp = new String[arr.length + 1];
for (int i = 0; i < arr.length; i++) {
temp[i] = arr[i];
}
return temp;
}
I was unsure what your plan was with your score variable, but this would be two ways to achieve your desired result.
I hope this helps.
public static void main(String [] args)
{
Scanner keyboard = new Scanner(System.in);
double score;
boolean loopNaming=true;
int i=0;
ArrayList<String> name = new ArrayList<>();
while(loopNaming==true)
{
System.out.printf("Enter name of student or done to finish: ");
String input = keyboard.next();
if(input.equals("done"))
{
loopNaming = false;
}
else
{ name.add(input);
System.out.println("Enter score: ");
score = keyboard.nextDouble();
}
i=i+1; //no need to use
}
System.out.println(name);
}
You should use a dynamic list because You can't resize an array in Java. The second point when the user gives "done", you should not put it in the list so check it before the insertion.
You declared your String array with size 0. that's why you cant add elements in to it.
import java.util.Scanner;
public class NameArray {
public static void main(String [] args){
Scanner keyboard = new Scanner(System.in);
double score[] = new double[10];
boolean loopNaming=true;
int i=0;
String namae;
String[] name = new String[10];
int count = 0;
while(loopNaming==true){
System.out.printf("Enter name of student or done to finish: ");
name[i] = keyboard.next();
if(name[i].equals("done")){
loopNaming = false;
}
else{
System.out.println("Enter score: ");
score[i] = keyboard.nextDouble();
count++;
}
i=i+1;
}
for(int j = 0; j < count; j++) {
System.out.println(name[j]+" "+score[j]);
}
}
}
Try this code or you can go for any other data structures.

Inserting values into two array using scanner class in java

import java.util.Scanner;
public class Demo3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many friends: ");
int numOfFriends = Integer.parseInt(scan.nextLine());
String arrayOfNames[] = new String[numOfFriends];
long income[] = new long[numOfFriends];
for (int i = 0; i < arrayOfNames.length; i++)
{
System.out.print("\nEnter the name of friend " + (i+1) + " : ");
arrayOfNames[i] = scan.nextLine();
for(int j = 0; j<arrayOfNames.length;j++)
{
System.out.print("\nEnter the income of friend " + (j+1) + " : ");
income[j] = scan.nextLong();
}
}
}
}
This is my code, I want to take input name from user then the income of that person then again the name of another person
The above code is not arranged properly, I think there's problem in the for loop the sample output should be like:
Enter how many friends: 2
Enter name of friend 1 : #############
Enter income of friend 1 : ##############
Enter name of friend 2 : #############
Enter income of friend 2 : ##############
You should take inner for loop out.
import java.util.Scanner;
public class Demo3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many friends: ");
int numOfFriends = Integer.parseInt(scan.nextLine());
String arrayOfNames[] = new String[numOfFriends];
long income[] = new long[numOfFriends];
for (int i = 0; i < arrayOfNames.length; i++)
{
System.out.print("\nEnter the name of friend " + (i+1) + " : ");
arrayOfNames[i] = scan.nextLine();
}
for(int j = 0; j<arrayOfNames.length;j++)
{
System.out.print("\nEnter the income of friend " + (j+1) + " : ");
income[j] = scan.nextLong();
}
scan.close();
}
}
Besides, if you want to enter them in order, you should just use one for loop
import java.util.Scanner;
public class Demo3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many friends: ");
int numOfFriends = Integer.parseInt(scan.nextLine());
String arrayOfNames[] = new String[numOfFriends];
long income[] = new long[numOfFriends];
for (int i = 0; i < arrayOfNames.length; i++)
{
System.out.print("\nEnter the name of friend " + (i+1) + " : ");
arrayOfNames[i] = scan.nextLine();
System.out.print("\nEnter the income of friend " + (i+1) + " : ");
income[i] = scan.nextLong();
scan.nextLine();
}
scan.close();
}
}
As a side note, don't forget to close() scanner after its task is completed.

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.
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
in void display() method you are calling super display() method so the super display() method is calling not that branch display method and add this.branchname

Categories