Find max value from an object [duplicate] - java

This question already has answers here:
Getting max value from an arraylist of objects?
(5 answers)
Closed 2 years ago.
I have super class Person that extends to 2 sup classes (Employee and Student), after I enter the Employee info such as (name, SSN, salary, Gendr) I want to find the Employee with the Max salary and type his\her info, but I don't know how to do that with objects !, if u may please give me a hint I'll be thankfull.
public abstract class Person {
private String name=" ";
private String gender=" ";
private long SSN;
public Person() {
}
public Person(String name, String gender, long SSN) {
this.name = name;
this.gender = gender;
this.SSN = SSN;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public long getSSN() {
return SSN;
}
public void setSSN(long SSN) {
this.SSN = SSN;
}
#Override
public String toString() {
return name + " " + gender + " " + SSN+ " ";
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if (this.SSN != other.SSN) {
return false;
}
if (!Objects.equals(this.gender, other.gender)) {
return false;
}
return true;
}
}
public class Employee extends Person {
private String type = " ";
private double salary;
public Employee() {
}
public Employee(double salary, String name, String gender, long SSN) {
super(name, gender, SSN);
this.salary = salary;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
#Override
public String toString() {
return super.toString()+ " " + type + " " + salary ;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Employee other = (Employee) obj;
if (Double.doubleToLongBits(this.salary) != Double.doubleToLongBits(other.salary)) {
return false;
}
if (!Objects.equals(this.type, other.type)) {
return false;
}
return true;
}
}
public class Test {
static void print(Person[] all, int count){
System.out.println("The allPersons array contains: ");
for (int i = 0; i <count; i++) {
System.out.println(all[i]);
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Person[] allPersons = new Person[5];
String name = " ";
long ssn=0;
String gender = " ";
int count = 0;
boolean s = true;
while(s){
System.out.println("Choose 1 to insert a new student");
System.out.println("Choose 2 to insert new employee");
System.out.println("Choose 3 to retrieve the maximum salary");
System.out.println("Choose 4 to retrieve all software engineering students");
System.out.println("Choose 0 to exit");
System.out.println("Please enter your choice: ");
int n = input.nextInt();
switch(n){
case 1:{ if(count == 5){System.out.println("Sorry, you reach the maximum length."); break;}
Student student = new Student();
System.out.println("Please enter Name: ");
name=input.next();
input.nextLine();
student.setName(name);
System.out.println("Please enter SSN: ");
ssn = input.nextLong();
student.setSSN(ssn);
System.out.println("Plase enter Gender: " );
gender = input.next();
student.setGender(gender);
System.out.println("Please enter major: ");
String major = input.next();
input.nextLine();
student.setMajor(major);
System.out.println("Plase inter Year of Regestration: ");
int year = input.nextInt();
student.setYearOfReg(year);
System.out.println("Please enter Studend ID: ");
long ID = input.nextLong();
student.setID(ID);
allPersons[count]=student;
count++;
print(allPersons,count);}
case 2 :{if (count==5) {System.out.println("Sorry, you reach the maximum length"); break;}
Employee emp = new Employee();
System.out.println("Please enter Name:");
name=input.next();
input.nextLine();
emp.setName(name);
System.out.println("Plese enter SSN:");
ssn=input.nextLong();
emp.setSSN(ssn);
System.out.println("Please enter Gender:");
gender=input.next();
emp.setGender(gender);
System.out.println("Plese enter type: ");
String type = input.next();
input.nextLine();
emp.setType(type);
System.out.println("Please enter Salary:");
double salary = input.nextDouble();
emp.setSalary(salary);
allPersons[count]=emp;
count++;
print(allPersons,count);
}
case 3: //Employee with max salary
case 0: System.out.println("Exit");s=false;break;
}
}
}
}

You can loop over the array of people and find the employee with the highest salary.
Employee highest = null;
for(int i = 0; i < count; i++){
if(allPersons[i] instanceof Employee && (highest == null || ((Employee) allPersons[i]).getSalary() > highest.getSalary())){
highest = (Employee) allPersons[i];
}
}
System.out.println(highest);

You can use a lambda function to obtain the max salary and check the type of person in the same function.
public Employee getMaxSalary(Person[] allPersons) {
return Arrays.asList(allPersons)
.stream()
.filter(Employee.class::isInstance) //Filter only the person who are Employee
.map(Employee.class::cast)
.max(Comparator.comparing(Employee::getSalary)) //Obtain only the max salary
.orElseThrow(NoSuchElementException::new);
}

Related

Taking user input for the objects

Question : Create a two objects of class Employee and check both are same or diffrent
Below code gives an error : Exception in thread "main" java.util.InputMismatchException
Only object e1 accepts values
class Employee {
String name;
int age;
char gender;
public Employee() {
super();
}
public Employee (String name, int age, char gender) {
this.gender = gender;
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
public class Source {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Employee e1 = new Employee();
//e1.name = sc.nextLine();
//e1.age = sc.nextInt();
//e1.gender = sc.next().charAt(0);
System.out.println(e1.name+" "+e1.age+" "+e1.gender);
Employee e2 = new Employee();
//e2.name = sc.nextLine();
//e2.age = sc.nextInt();
//e2.gender = sc.next().charAt(0);
System.out.println(e2.name+" "+e2.age+" "+e2.gender);
boolean isSame = e1.equals(e2);
if(e1.equals(e2)) {
System.out.println("Same");
}
else {
System.out.println("Different");
}
}
}
How to take user input or input from keyboard for objects e1 and e2?
I have made some changes to your Employee and Source classes, take a look.
public class Source {
Scanner sc = null;
public static void main(String[] args) {
Source source = new Source();
Employee e1 = source.getEmployee();
Employee e2 = source.getEmployee();
if(e1.equals(e2)) {
System.out.println("Same");
}
else {
System.out.println("Different");
}
source.closeScanner();
}
public Source() {
sc = new Scanner(System.in);
}
public void closeScanner() {
sc.close();
}
public Employee getEmployee() {
Employee e = new Employee();
System.out.print("Enter Name: ");
e.name = sc.next();
System.out.print("Enter Age: ");
e.age = sc.nextInt();
System.out.print("Enter Gender: ");
e.gender = sc.next().charAt(0);
System.out.println(e.getName() + " " + e.getAge() + " " + e.getGender());
return e;
}
}
public class Employee {
String name;
int age;
char gender;
public Employee() {
super();
int age = -1;
}
public Employee (String name, int age, char gender) {
this.gender = gender;
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + gender;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (gender != other.gender)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
Please note that I've added the following two methods
public int hashCode()
and
public boolean equals(Object obj)
to make it possible to compare the two instances of the employee object

How to search and display an object by ID number in an Arraylist java?

i really need help with my program. I'm new to Arraylist and I have no idea how can i search and display an object by its ID number.
So the program has 4 class: Main, Person (parent class) Student and Employee (both are child of Person)
Now my main class has a menu that will do the following:
Add Student ( ID must be unique for each stud )
Add Employee ( ID must be unique for each emp )
Search Student ( By Student ID then display it)
Search Employee ( By Employee No then display it)
Display All ( Display All Stud and Employee )
Quit
I have done this before with array searching and displaying but now we have to use Arraylist which is better but im new to it.
Any help and suggestion is highly appreciated. Thank you!
Here's my code:
Main Class:
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Student> stud = new ArrayList<Student>();
ArrayList<Employee> emp = new ArrayList<Employee>();
while (true) {
int select;
System.out.println("Menu");
System.out.println("1. Add Student");
System.out.println("2. Add Employee");
System.out.println("3. Search Student");
System.out.println("4. Search Employee");
System.out.println("5. Display All");
System.out.println("6. Quit");
select = sc.nextInt();
switch (select) {
case 1:
addStud(stud);
break;
case 2:
addEmp(emp);
break;
case 3:
srchStud();
break;
case 4:
srchEmp();
case 5:
displayAll(stud, emp);
break;
case 6:
return;
default:
System.out.println("Invalid Option");
}
}
}
public static void addStud(ArrayList<Student> stud) {
String name, address, course;
int age, cNum, studNum, year;
int addMore;
System.out.println("ADD STUDENT");
do {
System.out.println("Student No: ");
studNum = sc.nextInt();
sc.nextLine();
for(int x = 0; x < stud.size(); x++){ // Please help fix, this accepts another ID if already existing to prevent duplicate
if(studNum == stud[x].getStudNum()) { // this code works with array of object but not on arraylist,
System.out.println("The Student ID: " +studNum+ " already exist.\nEnter New Student ID: ");
studNum = sc.nextInt();
sc.nextLine();
x = -1;
}
}
System.out.println("Name: ");
name = sc.nextLine();
System.out.println("Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.println("Address: ");
address = sc.nextLine();
System.out.println("Contact No: ");
cNum = sc.nextInt();
sc.nextLine();
System.out.println("Course: ");
course = sc.nextLine();
System.out.println("Year: ");
year = sc.nextInt();
sc.nextLine();
stud.add(new Student(name, address, age, cNum, studNum, year, course));
System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);
}
public static void addEmp(ArrayList<Employee> emp) {
String name, address, position;
int age, cNum, empNum;
double salary;
int addMore;
System.out.println("ADD Employee");
do {
System.out.println("Employee No: ");
empNum = sc.nextInt();
sc.nextLine();
for(int x = 0; x < emp.size(); x++){
if(empNum == emp[x].getEmpNum()) {
System.out.println("The Employee ID: " +empNum+ " already exist.\nEnter New Student ID: ");
empNum = sc.nextInt();
sc.nextLine();
x = -1;
}
}
System.out.println("Name: ");
name = sc.nextLine();
System.out.println("Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.println("Address: ");
address = sc.nextLine();
System.out.println("Contact No: ");
cNum = sc.nextInt();
sc.nextLine();
System.out.println("Position: ");
position = sc.nextLine();
System.out.println("Salary: ");
salary = sc.nextInt();
sc.nextLine();
emp.add(new Employee(name, address, age, cNum, empNum, salary, position));
System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);
}
public static void displayAll(ArrayList<Student> stud, ArrayList<Employee> emp){ // Definitely not working with Arraylist
System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
for (int x = 0; x < stud.size(); x++) {
System.out.println(stud[x].getStudNum() + "\t\t\t\t" + stud[x].getName() + "\t\t\t\t" + stud[x].getCourse() + "\t\t\t\t" + stud[x].getYear());
}
}
}
Person Class :
public class Person {
private String name, address;
private int age, cNum;
public Person() {
}
public Person(String name, String address, int age, int cNum) {
this.name = name;
this.address = address;
this.age = age;
this.cNum = cNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getcNum() {
return cNum;
}
public void setcNum(int cNum) {
this.cNum = cNum;
}
}
Student Class :
public class Student extends Person{
private int studNum, year;
private String course;
public Student(String name, String address, int age, int cNum, int studNum, int year, String course) {
super(name, address, age, cNum);
this.studNum = studNum;
this.year = year;
this.course = course;
}
public int getStudNum() {
return studNum;
}
public void setStudNum(int studNum) {
this.studNum = studNum;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
Employee Class :
public class Employee extends Person {
private int empNum;
private double salary;
private String position;
public Employee(String name, String address, int age, int cNum, int empNum, double salary, String position) {
super(name, address, age, cNum);
this.empNum = empNum;
this.salary = salary;
this.position = position;
}
public int getEmpNum() {
return empNum;
}
public void setEmpNum(int empNum) {
this.empNum = empNum;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
If you are not limited to use ArrayList, better performance will be achieved using a Map.
Map<Integer, Employee> employeeIndex = new HashMap<>();
//put
employeeIndex.put(employeeId, empployee);
//get
employeeIndex.get(employeeId);
In case, you need to use ArrayList, you can use filter:
List<Student> students = new ArrayList<Student>();
List<Student> filteredStudent = students.stream()
.filter(student -> student.getStudNum() == student_id)
.collect(Collectors.toList());
just this way if you are searching for a student the id is 12
ArrayList<Integer> students;
students.forEach(stu -> {if ( stu.getStudNum() == 2) System.out.println(stu.getName());});

I am making a program to display error message whenever an empty space or blank space is entered

I am making a program to display error message whenever the input was blank or empty space and continues until desired input was entered.
Here's my current code:
public static Person getInput ()
{
Scanner data = new Scanner(System.in);
Person p = new Person();
boolean isEmpty = false;
int personAge = 0;
String personName;
char personGender;
while (isEmpty) {
try {
System.out.println("Name: ");
personName = data.nextLine();
personName.toUpperCase();
if (personName.isEmpty()) {
System.out.println("Please enter your name. ");
} else {
p.setName(personName);
}
System.out.println("AGE: ");
personAge = Integer.parseInt(data.nextLine());
if (personAge >= 1 && personAge <= 50)
{
System.out.println(" ");
} else {
System.err.println("Please enter a number from 1 - 50. ");
}
} catch (Exception e) {
System.err.println("Please enter your age.");
data.next();
} finally {
}
System.out.println("GENDER: ");
personGender = data.next().charAt(0);
p.setGender(personGender);
}
return p;
}
I am still a bit lost if I got it right or I need to correct some other things still. Thanks.
You should try something like with looping:
do {
System.out.println("Name: ");
personName = data.nextLine();
if (personName.isEmpty()) {
System.out.println("Please enter your name. ");
}else {
personName = personName.toUpperCase();//String is immutable
p.setName(personName);
}
} while (personName.isEmpty())
I changed your program a little bit so that it runs. If you ask a question it is more convenient to help you when you post a runnable example.
Here is what i did:
package person;
import java.util.Scanner;
public class Person {
private String name = "";
private char gender = ' ';
private int age = 0;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void getInput() {
Scanner data = new Scanner(System.in);
while (getName().trim().isEmpty()) {
System.out.print("Name (not empty): ");
setName(data.nextLine());
setName(getName().toUpperCase());
}
while (getAge() < 1 | getAge() > 50) {
System.out.print("AGE (1 - 50): ");
setAge(Integer.parseInt(data.nextLine()));
}
System.out.print("GENDER (m/f): ");
setGender(data.next().charAt(0));
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
#Override
public String toString() {
return String.format("%s (%s) is %d year(s) old", getName(), getGender(), getAge());
}
public static void main(String[] args) {
Person p = new Person();
p.getInput();
System.out.println(p.toString());
}
}
I created getter and setter methods which is common to encapsulate classes from each other and improve the possibility for re-use.
As SMA has already recommended it is handy to use while loops to ask the user again if the input was not what you expected.
I would also recommend to not just ask 'AGE:' but 'AGE (1-50):' which guides the user through the inputs.
Sorry for also answering questions you didn't ask (;
Here's my updated codes. I would like to know if I'm doing it right this time.
public class PersonApp {
public static void main(String [] args) {
System.out.println("Enter 'exit' in name to quit app");
while(true) {
try {
Person p = Person.getInput();
if(p.getName().equalsIgnoreCase("exit")) break;
}
catch(Exception e) {
System.out.println("Error on input: " + e.getMessage());
}
}
}
}
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Person {
private static String name;
private static int age;
private static char gender;
public String getName ()
{
return name;
}
public void setName (String name)
{
this.name = name;
}
public int getAge ()
{
return age;
}
public void setAge (int age)
{
this.age = age;
}
public char getGender ()
{
return gender;
}
public void setGender (char gender)
{
this.gender = gender;
}
public static Person getInput ()
{
Scanner data = new Scanner(System.in);
Person p = new Person();
boolean isEmpty = false;
while (p.getName().trim().isEmpty()) {
try {
System.out.println ("Please enter your name.");
p.setName(data.next());
p.setName(p.getName().toUpperCase());
break;
}
catch (Exception e) {
System.err.println(" Input invalid. ");
p.setName(data.next());
}
}
while (p.getAge() >= 1 && p.getAge() <=50) {
try {
System.out.println("AGE (1-50): ");
p.setAge(Integer.parseInt(data.nextLine()));
if (p.getAge() >= 1 && p.getAge() <=50)
{
p.setAge(p.getAge());
break;
}else {
System.err.println("Please enter a number from 1 - 50. ");
}
}
catch (Exception e) {
System.err.println(" Input invalid. ");
p.setAge(Integer.parseInt(data.nextLine()));
}
}
while (p.getGender() == 'F' || p.getGender() == 'f' || p.getGender() == 'M' || p.getGender() == 'm') {
try {
System.out.println("GENDER (M/F): ");
p.setGender(data.next().charAt(0));
if (p.getGender() == 'F' || p.getGender() == 'f' ||
p.getGender() == 'M' || p.getGender() == 'm') {
p.setGender(p.getGender());
break;
} else {
System.out.println ("Please enter M for male and F for female. ");
}
}
catch (Exception e) {
System.err.println(" Input invalid. ");
p.setGender(data.next().charAt(0));
}
}
return p;
}
}

How to search for an element in an array? and How to add variables with declared methods into an array list?

I have 2 major troubles (that I'm aware of) with this program. Firstly, I don't know how to get FinalGrade and LetterGrade into the array. Secondly, I don't know how to use last name and first name to search for a student in the array. Let me know if you find other problems with my program. Thanks
This is the 1st class
package student;
public class Person {
protected String FirstName, LastName;
//Constructor
public Person(String FirstName, String LastName) {
this.FirstName = FirstName;
this.LastName = LastName;
}
//Getters
public String getFirstName() {
return FirstName;
}
public String getLastName() {
return LastName;
}
//Setters
public void setFirstName(String FirstName) {
this.FirstName = FirstName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
}
This is the 2nd class:
package student;
import java.util.ArrayList;
import java.util.Scanner;
public class Student extends Person{
private int HomeworkAve, QuizAve, ProjectAve, TestAve;
private double FinalGrade;
private String LetterGrade;
//Constructor for the averages
public Student(int HomeworkAve, int QuizAve, int ProjectAve, int TestAve, String FirstName, String LastName)
{
super(FirstName, LastName);
this.HomeworkAve = HomeworkAve;
this.QuizAve = QuizAve;
this.ProjectAve = ProjectAve;
this.TestAve = TestAve;
}
//Method to calculate final grade and letter grade
//Final grade calculation
public double CalcGrade (int HomeworkAve, int QuizAve, int ProjectAve, int TestAve)
{
FinalGrade = (double)(0.15*HomeworkAve + 0.05*QuizAve + 0.4 * ProjectAve + 0.4*TestAve);
return FinalGrade;
}
//Letter grade calculation
public String CalcGrade ( double FinalGrade)
{
if ( FinalGrade >= 90.00)
LetterGrade="A";
else if(FinalGrade >= 80.00)
LetterGrade="B";
else if(FinalGrade>=70.00)
LetterGrade="C";
else if(FinalGrade>=60.00)
LetterGrade="D";
else LetterGrade="F";
return LetterGrade;
}
public String getFullName (String FirstName,String LastName)
{
String str1 = FirstName;
String str2 = LastName;
String FullName = str1+","+str2;
return FullName;
}
public Student(int HomeworkAve, int QuizAve, int ProjectAve, int TestAve, double FinalGrade, String LetterGrade, String FirstName, String LastName) {
super(FirstName, LastName);
this.HomeworkAve = HomeworkAve;
this.QuizAve = QuizAve;
this.ProjectAve = ProjectAve;
this.TestAve = TestAve;
this.FinalGrade = FinalGrade;
this.LetterGrade = LetterGrade;
}
//Setters for this student class
public void setHomeworkAve(int HomeworkAve) {
this.HomeworkAve = HomeworkAve;
}
public void setQuizAve(int QuizAve) {
this.QuizAve = QuizAve;
}
public void setProjectAve(int ProjectAve) {
this.ProjectAve = ProjectAve;
}
public void setTestAve(int TestAve) {
this.TestAve = TestAve;
}
public void setFinalGrade(int FinalGrade) {
this.FinalGrade = FinalGrade;
}
public void setLetterGrade(String LetterGrade) {
this.LetterGrade = LetterGrade;
}
//Getters for this student class
public int getHomeworkAve() {
return HomeworkAve;
}
public int getQuizAve() {
return QuizAve;
}
public int getProjectAve() {
return ProjectAve;
}
public int getTestAve() {
return TestAve;
}
public double getFinalGrade() {
return FinalGrade;
}
public String getLetterGrade() {
return LetterGrade;
}
public void DisplayGrade (){
System.out.println(FirstName+" "+LastName+"/nFinal Grade: "+FinalGrade+"/nLetter Grade: "+ LetterGrade);
}
public static void main(String[] args){
Scanner oScan = new Scanner(System.in);
Scanner iScan = new Scanner(System.in);
boolean bContinue = true;
int iChoice;
ArrayList<Student> students = new ArrayList<>();
while (bContinue == true)
{
//The menu
System.out.println("1. New Class List");
System.out.println("2. Search for a Student");
System.out.println("3. Exit");
System.out.println("Choose an item");
iChoice = iScan.nextInt();
//The 1st case: when the user wants to enter the new list
if (iChoice == 1){
System.out.println("Enter the number of students");
int numberOfStudents = iScan.nextInt();
for(int iCount = 0;iCount < numberOfStudents;){
System.out.println("Enter the name for Student " + ++iCount);
System.out.println("Enter First Name");
String FirstName = oScan.nextLine();
System.out.println();
System.out.println("Enter Last Name");
String LastName = oScan.nextLine();
System.out.println();
System.out.println("Enter Homework Average");
int HomeworkAve = iScan.nextInt();
System.out.println();
System.out.println("Enter Quiz Average");
int QuizAve = iScan.nextInt();
System.out.println();
System.out.println("Enter Project Average");
int ProjectAve = iScan.nextInt();
System.out.println();
System.out.println("Enter Test Average");
int TestAve = iScan.nextInt();
System.out.println();
How to get FinalGrade and LetterGrade??
Student hobbit = new Student(HomeworkAve,QuizAve, ProjectAve,TestAve,FirstName, LastName);
students.add(hobbit);
}
}
//The 2nd case: when the user wants to search for a student
else if (iChoice == 2)
{
System.out.println("Enter First Name");
String FirstName = oScan.nextLine();
System.out.println();
System.out.println("Enter Last Name");
String LastName = oScan.nextLine();
System.out.println();
Here is my revised code to find the student but I don't know how to print the array after I found it
int i = 0;
for (Student student : students) {
if (FirstName != null & LastName != null);{
if (student.getFirstName().equals(FirstName) && student.getLastName().equals(LastName)) {
System.out.println(students.get(i)); //this doesn't work
break;
}
i++;
}
}
//The 3r case: when the user wants to exit
else if (iChoice == 3)
{
bContinue = false;
}
}
}
}
You have an ArrayList of type Student and you are doing indexOf on a variable of type String. You will have to traverse the entire ArrayList and then do a comparison to find out if the student name mathces. Sample code:
int i = 0;
for (Student student : students) {
// Add null checks if first/last name can be null
if (student.getFirstName().equals(firstName) && student.getLastName().equals(lastName)) {
System.out.println("Found student at " + i);
break;
}
i++;
}
FinalGrade and LetterGrade are variables in each object, I think you are mistaken about the concept of an Array/ArrayList. The ArrayList only holds each Student object, not the variable in each object. You can go through each student and get their finalGrade and letterGrade with the get* functions. Before doing this, you should make a call to CalcGrade methods so the grades are calculated.

arrayList of type Student.Graduate has incorrect types

Code with line numbers here
package project5_test2;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* #author David
*/
public class Project5_test2 {
public static Object Student;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
// initialize an arary that holds N objects of type Student
int end = 1;
ArrayList<Student> studentList = new ArrayList<>();
// read in the data
for (int y = 0; y < end;) {
String nextMove = nextMove();
for (int i = 0; i < end;) {
if ("add".equalsIgnoreCase(nextMove)) {
System.out.println("Student Input: ");
String firstName = getFirstName();
String lastName = getLastName();
int uid = getUid();
String stringUid = Integer.toString(uid);
StudentType type = inputStudentType();
if (type == StudentType.UNDECLARED) {
ClassStanding standing = inputClassStanding();
studentList.add(new Student(firstName, lastName, uid, type, standing ));
}
if (type == StudentType.UNDERGRADUATE) {
ClassStanding standing = inputClassStanding();
Major major = inputMajor();
double overallGpa = getOverallGPA();
String overallGpaDouble = Double.toString(overallGpa);
double majorGpa = getOverallGPA();
String majorGpaDouble = Double.toString(majorGpa);
studentList.add(new Student(firstName, lastName, uid, type, standing));
}
if (type == StudentType.GRADUATE) {
boolean thesis = getThesisStatus();
String thesisString = Boolean.toString(thesis);
ClassStanding studyType = getStudy();
String profName = getProfName();
studentList.add(new Student.Graduate( thesis, studyType, profName));
}
} i++;
}
if ("remove".equalsIgnoreCase(nextMove)) {
System.out.println("derp");
}
if ("save".equalsIgnoreCase(nextMove)) {
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
y++;
}
}
}
enum ClassStanding {
FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, UNKNOWN,
MASTERS_STUDIES, PHD_STUDIES, NO_STANDING
};
enum StudentType {
UNDERGRADUATE, GRADUATE, UNDECLARED
};
enum Major {
CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN
};
public static class Student {
public String firstName; // first name
public String lastName;
public double uid; // last name
public StudentType type;
public ClassStanding standing;
public Student(Student orig) {
}
// construct a new student with given fields
public Student(String firstName, String lastName, Integer newUid, StudentType type, ClassStanding standing) {
this.firstName = firstName;
this.lastName = lastName;
this.uid = newUid;
this.type = type;
this.standing = standing;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
//set type
public void setType(StudentType type) {
this.type = type;
}
//return type
public StudentType getType() {
return type;
}
public void setStanding(ClassStanding standing) {
this.standing = standing;
}
//return type
public ClassStanding getStanding() {
return standing;
}
// return a string representation of the invoking object
public String toString() {
return firstName + " " + lastName + " " + uid + " " + type + " " + standing;
}
public static class Graduate extends Student {
public boolean thesis;
public ClassStanding study;
public String profName;
public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
super(orig);
}
public boolean getThesis() {
return thesis;
}
public void setThesis(Boolean thesis) {
this.thesis = thesis;
}
public ClassStanding getStudy() {
return study;
}
public void setStudy(ClassStanding study) {
this.study = study;
}
public String getProfName() {
return profName;
}
public void setProfName(String profName) {
this.profName = profName;
}
public String toString() {
return thesis + " " + study + " " + profName;
}
}
public static class UnderGraduate extends Student {
public Major major;
public Double GPA;
public UnderGraduate(Student orig, Major major, Double GPA) {
super(orig);
}
public void setMajor(Major major) {
this.major = major;
}
//return type
public Major getmMajor() {
return major;
}
public void setOverallGPA(Integer uid) {
this.uid = uid;
}
public double getOverallGPA() {
return uid;
}
public String toString() {
return major + " " + uid ;
}
}
}
public static String getFirstName() {
Scanner input = new Scanner(System.in);
String firstName;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter the students first name: ");
firstName = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a string please enter another first name!");
input.next();
continue;
}
}
return firstName;
}
public static String getLastName() {
Scanner input = new Scanner(System.in);
String lastName;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter a students last name: ");
lastName = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a string please enter another last name!");
input.next();
continue;
}
}
return lastName;
}
public static Integer getUid() {
Scanner input = new Scanner(System.in);
int uid;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter a uid number: ");
uid = input.nextInt();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a number or an integer!");
input.next();
continue;
}
}
return uid;
}
public static StudentType inputStudentType() {
Scanner input = new Scanner(System.in);
String type;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Is the student a graduate, undergraduate, or, undeclared: ");
type = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("False input. Is the student a graduate, undergraduate, or, undeclared: ");
type = input.next();
continue;
}
}
if ("undergraduate".equalsIgnoreCase(type)) {
StudentType status = StudentType.UNDERGRADUATE;
return status;
}
if ("graduate".equalsIgnoreCase(type)) {
StudentType status = StudentType.GRADUATE;
return status;
}
if ("undeclared".equalsIgnoreCase(type)) {
StudentType status = StudentType.UNDECLARED;
return status;
}
return null;
}
public static String nextMove() {
Scanner input = new Scanner(System.in);
String nextMove;
while (true) {
// prompt the user to enter if there are more characers
try {
System.out.print("What now? Add, Sort, Remove, Save: ");
nextMove = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("More input? true or false(type true or false): ");
input.next();
continue;
}
}
return nextMove;
}
public static ClassStanding inputClassStanding() {
Scanner input = new Scanner(System.in);
String type;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter the students class standing: ");
type = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("False input. Input the students class standing: ");
type = input.next();
continue;
}
}
if ("Freshman".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.FRESHMAN;
return standing;
}
if ("sophomore".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.SOPHOMORE;
return standing;
}
if ("junior".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.JUNIOR;
return standing;
}
if ("senior".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.SENIOR;
return standing;
}
if ("unknown".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.UNKNOWN;
return standing;
}
return null;
}
public static Major inputMajor() {
Scanner input = new Scanner(System.in);
String major;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
major = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("False input. Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
major = input.next();
continue;
}
}
if ("cs".equalsIgnoreCase(major)) {
Major finalMajor = Major.CS;
return finalMajor;
}
if ("ceg".equalsIgnoreCase(major)) {
Major finalMajor = Major.CEG;
return finalMajor;
}
if ("ee".equalsIgnoreCase(major)) {
Major finalMajor = Major.EE;
return finalMajor;
}
if ("ise".equalsIgnoreCase(major)) {
Major finalMajor = Major.ISE;
return finalMajor;
}
if ("bme".equalsIgnoreCase(major)) {
Major finalMajor = Major.BME;
return finalMajor;
}
if ("ME".equalsIgnoreCase(major)) {
Major finalMajor = Major.ME;
return finalMajor;
}
if ("met".equalsIgnoreCase(major)) {
Major finalMajor = Major.MET;
return finalMajor;
}
if ("unknown".equalsIgnoreCase(major)) {
Major finalMajor = Major.UNKNOWN;
return finalMajor;
}
return null;
}
public static Double getOverallGPA() {
Scanner input = new Scanner(System.in);
double overallGpa;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter the students overall GPA: ");
overallGpa = input.nextDouble();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a number or an integer! Please enter the students overall GPA:");
input.next();
continue;
}
}
return overallGpa;
}
public static Double getMajorlGPA() {
Scanner input = new Scanner(System.in);
double majorGpa;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter the students major GPA: ");
majorGpa = input.nextDouble();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a number or an integer! Please enter the students major GPA:");
input.next();
continue;
}
}
return majorGpa;
}
public static boolean getThesisStatus() {
Boolean thesis;
Scanner input = new Scanner(System.in);
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter TRUE for having a thesis option else FALSE: ");
thesis = input.nextBoolean();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a boolean response. True or false, is there a thesis: ");
input.next();
continue;
}
}
return thesis;
}
public static ClassStanding getStudy() {
String study = null;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 1;) {
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter Masters for Master Studies or Phd for Phd studies: ");
study = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a valid response. Enter Masters for Master Studies or Phd for Phd studies: ");
input.next();
continue;
}
}
if ("masters".equalsIgnoreCase(study)) {
ClassStanding standing = ClassStanding.MASTERS_STUDIES;
return standing;
}
if ("phd".equalsIgnoreCase(study)) {
ClassStanding standing = ClassStanding.PHD_STUDIES;
return standing;
}
}
return null;
}
public static String getProfName() {
String name;
Scanner input = new Scanner(System.in);
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter the name of the major professor: ");
name = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a valid response. Enter the name of the major professor: ");
input.next();
continue;
}
}
return name;
}
}
the issue is currently shown on line 54, the same error will occur on line 46.
When writing to the ArrayList studentList I need to include a variable of Student type.
This issue is likely in my constructors for my subclasses, but I'm not totally familiar with subclasses.
Student.Graduate receives a Student as the first parameter in the constructor - you forgot to send it thus the compilation error.
If you'll replace line 54 with:
studentList.add(new Student.Graduate(
new Student(firstName, lastName, uid, type,null ),
thesis, studyType, profName));
you won't get that compilation error.
That said - there are other problems in the code, see fixed code below:
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
public class Project5_test2 {
public static Object Student;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
// initialize an arary that holds N objects of type Student
int end = 1;
ArrayList<Student> studentList = new ArrayList<Student>();
// read in the data
for (int y = 0; y < end;) {
String nextMove = nextMove();
for (int i = 0; i < end;) {
if ("add".equalsIgnoreCase(nextMove)) {
System.out.println("Student Input: ");
String firstName = getFirstName();
String lastName = getLastName();
int uid = getUid();
String stringUid = Integer.toString(uid);
StudentType type = inputStudentType();
ClassStanding standing = inputClassStanding();
Student base = new Student(firstName, lastName, uid, type, standing);
if (type == StudentType.UNDECLARED) {
studentList.add(base);
}
else if (type == StudentType.UNDERGRADUATE) {
Major major = inputMajor();
double overallGpa = getOverallGPA();
String overallGpaDouble = Double.toString(overallGpa);
double majorGpa = getOverallGPA();
String majorGpaDouble = Double.toString(majorGpa);
studentList.add(base);
}
else if (type == StudentType.GRADUATE) {
boolean thesis = getThesisStatus();
//String thesisString = Boolean.toString(thesis);
ClassStanding studyType = getStudy();
String profName = getProfName();
studentList.add(new Student.Graduate(base, thesis, studyType, profName));
}
} i++;
}
if ("remove".equalsIgnoreCase(nextMove)) {
System.out.println("derp");
}
if ("save".equalsIgnoreCase(nextMove)) {
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
y++;
}
}
}
enum ClassStanding {
FRESHMAN, SOPHOMORE, JUNIOR, SENIOR, UNKNOWN,
MASTERS_STUDIES, PHD_STUDIES, NO_STANDING
};
enum StudentType {
UNDERGRADUATE, GRADUATE, UNDECLARED
};
enum Major {
CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN
};
public static class Student {
public String firstName; // first name
public String lastName;
public double uid; // last name
public StudentType type;
public ClassStanding standing;
public Student(Student orig) {
this.firstName = orig.firstName;
this.lastName = orig.lastName;
this.uid = orig.uid;
this.type = orig.type;
this.standing = orig.standing;
}
// construct a new student with given fields
public Student(String firstName, String lastName, Integer newUid, StudentType type, ClassStanding standing) {
this.firstName = firstName;
this.lastName = lastName;
this.uid = newUid;
this.type = type;
this.standing = standing;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
//set type
public void setType(StudentType type) {
this.type = type;
}
//return type
public StudentType getType() {
return type;
}
public void setStanding(ClassStanding standing) {
this.standing = standing;
}
//return type
public ClassStanding getStanding() {
return standing;
}
// return a string representation of the invoking object
public String toString() {
return firstName + " " + lastName + " " + uid + " " + type + " " + standing;
}
public static class Graduate extends Student {
public boolean thesis;
public ClassStanding study;
public String profName;
public Graduate(Student orig, boolean isThesis, ClassStanding study, String profName) {
super(orig);
thesis = isThesis;
this.study = study;
this.profName = profName;
}
public boolean getThesis() {
return thesis;
}
public void setThesis(Boolean thesis) {
this.thesis = thesis;
}
public ClassStanding getStudy() {
return study;
}
public void setStudy(ClassStanding study) {
this.study = study;
}
public String getProfName() {
return profName;
}
public void setProfName(String profName) {
this.profName = profName;
}
public String toString() {
return super.toString() + thesis + " " + study + " " + profName;
}
}
public static class UnderGraduate extends Student {
public Major major;
public Double GPA;
public UnderGraduate(Student orig, Major major, Double GPA) {
super(orig);
}
public void setMajor(Major major) {
this.major = major;
}
//return type
public Major getmMajor() {
return major;
}
public void setOverallGPA(Integer uid) {
this.uid = uid;
}
public double getOverallGPA() {
return uid;
}
public String toString() {
return major + " " + uid ;
}
}
}
public static String getFirstName() {
Scanner input = new Scanner(System.in);
String firstName;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter the students first name: ");
firstName = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a string please enter another first name!");
input.next();
continue;
}
}
return firstName;
}
public static String getLastName() {
Scanner input = new Scanner(System.in);
String lastName;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter a students last name: ");
lastName = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a string please enter another last name!");
input.next();
continue;
}
}
return lastName;
}
public static Integer getUid() {
Scanner input = new Scanner(System.in);
int uid;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter a uid number: ");
uid = input.nextInt();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a number or an integer!");
input.next();
continue;
}
}
return uid;
}
public static StudentType inputStudentType() {
Scanner input = new Scanner(System.in);
String type;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Is the student a graduate, undergraduate, or, undeclared: ");
type = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("False input. Is the student a graduate, undergraduate, or, undeclared: ");
type = input.next();
continue;
}
}
if ("undergraduate".equalsIgnoreCase(type)) {
StudentType status = StudentType.UNDERGRADUATE;
return status;
}
if ("graduate".equalsIgnoreCase(type)) {
StudentType status = StudentType.GRADUATE;
return status;
}
if ("undeclared".equalsIgnoreCase(type)) {
StudentType status = StudentType.UNDECLARED;
return status;
}
return null;
}
public static String nextMove() {
Scanner input = new Scanner(System.in);
String nextMove;
while (true) {
// prompt the user to enter if there are more characers
try {
System.out.print("What now? Add, Sort, Remove, Save: ");
nextMove = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("More input? true or false(type true or false): ");
input.next();
continue;
}
}
return nextMove;
}
public static ClassStanding inputClassStanding() {
Scanner input = new Scanner(System.in);
String type;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter the students class standing: ");
type = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("False input. Input the students class standing: ");
type = input.next();
continue;
}
}
if ("Freshman".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.FRESHMAN;
return standing;
}
if ("sophomore".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.SOPHOMORE;
return standing;
}
if ("junior".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.JUNIOR;
return standing;
}
if ("senior".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.SENIOR;
return standing;
}
if ("unknown".equalsIgnoreCase(type)) {
ClassStanding standing = ClassStanding.UNKNOWN;
return standing;
}
return null;
}
public static Major inputMajor() {
Scanner input = new Scanner(System.in);
String major;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
major = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("False input. Enter the students major (CS, CEG, EE, ISE, BME, ME, MET, UNKNOWN): ");
major = input.next();
continue;
}
}
if ("cs".equalsIgnoreCase(major)) {
Major finalMajor = Major.CS;
return finalMajor;
}
if ("ceg".equalsIgnoreCase(major)) {
Major finalMajor = Major.CEG;
return finalMajor;
}
if ("ee".equalsIgnoreCase(major)) {
Major finalMajor = Major.EE;
return finalMajor;
}
if ("ise".equalsIgnoreCase(major)) {
Major finalMajor = Major.ISE;
return finalMajor;
}
if ("bme".equalsIgnoreCase(major)) {
Major finalMajor = Major.BME;
return finalMajor;
}
if ("ME".equalsIgnoreCase(major)) {
Major finalMajor = Major.ME;
return finalMajor;
}
if ("met".equalsIgnoreCase(major)) {
Major finalMajor = Major.MET;
return finalMajor;
}
if ("unknown".equalsIgnoreCase(major)) {
Major finalMajor = Major.UNKNOWN;
return finalMajor;
}
return null;
}
public static Double getOverallGPA() {
Scanner input = new Scanner(System.in);
double overallGpa;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter the students overall GPA: ");
overallGpa = input.nextDouble();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a number or an integer! Please enter the students overall GPA:");
input.next();
continue;
}
}
return overallGpa;
}
public static Double getMajorlGPA() {
Scanner input = new Scanner(System.in);
double majorGpa;
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Please enter the students major GPA: ");
majorGpa = input.nextDouble();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a number or an integer! Please enter the students major GPA:");
input.next();
continue;
}
}
return majorGpa;
}
public static boolean getThesisStatus() {
Boolean thesis;
Scanner input = new Scanner(System.in);
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter TRUE for having a thesis option else FALSE: ");
thesis = input.nextBoolean();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a boolean response. True or false, is there a thesis: ");
input.next();
continue;
}
}
return thesis;
}
public static ClassStanding getStudy() {
String study = null;
Scanner input = new Scanner(System.in);
for (int i = 0; i < 1;) {
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter Masters for Master Studies or Phd for Phd studies: ");
study = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a valid response. Enter Masters for Master Studies or Phd for Phd studies: ");
input.next();
continue;
}
}
if ("masters".equalsIgnoreCase(study)) {
ClassStanding standing = ClassStanding.MASTERS_STUDIES;
return standing;
}
if ("phd".equalsIgnoreCase(study)) {
ClassStanding standing = ClassStanding.PHD_STUDIES;
return standing;
}
}
return null;
}
public static String getProfName() {
String name;
Scanner input = new Scanner(System.in);
while (true) {
//prompt the user to enter the popularity number for each character
try {
System.out.print("Enter the name of the major professor: ");
name = input.next();
break;
//error handling
} catch (InputMismatchException e) {
System.out.println("Not a valid response. Enter the name of the major professor: ");
input.next();
continue;
}
}
return name;
}
}
UPDATE
If you don't want a graduate student to have a class-standing, you can set it to null, change the following:
if (type == StudentType.GRADUATE) {
boolean thesis = getThesisStatus();
String thesisString = Boolean.toString(thesis);
ClassStanding studyType = null;//getStudy(); <-- change this line
String profName = getProfName();
studentList.add(new Student.Graduate( thesis, studyType, profName));
}

Categories