This is my first time posting on StackOverflow, but I have visited many times. I am taking a computer science class and I ran into a problem that I have not been able to resolve through research. The assignment is to create a NBATeam class with instance variables and constructors and a NBA client that receives parameters of the names of players from the user and passes them to an array in the first class.
So the problem that I ran into is when I run my program, and I enter player names and pass them to the array, the first array of names are deleted. The names entered into the most recent array are fine, however.
I apologize if my terminology is wrong I am still a newb!
Thank you so much for any help you can give me.
I will insert my code below. My problem is where it says [null]. Also I entered bob for the first team and brian for the second team but bob is in the second team's array.
Please help!
Create NBA team of Heats
Add a player to team Heats? (yes/no)
yes
Enter the name of the player:
bob
Add one more player to Heats?
no
Create NBA team of Spurs
Add a player to team Spurs? (yes/no)
yes
Enter the name of the player:
brian
Add another player to Spurs?
no
***Heat Wins!***
Heats[null]Wins: 4 Loses: 0
Spurs[bob]Wins: 0 Loses: 4
import java.util.*;
public class NBA {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String ifAddPlayer, playerName = null;
//construct Team Heat
System.out.println("Create NBA team of Heats");
NBATeam heats = new NBATeam("Heats");
System.out.println("Add a player to team Heats? (yes/no)");
ifAddPlayer = input.next();
while(ifAddPlayer.equalsIgnoreCase("yes")) {
System.out.println("Enter the name of the player: ");
heats.addPlayer(playerName);
playerName = input.next();
System.out.println("Add one more player to Heats?");
ifAddPlayer = input.next();
}
//construct team Spurs.
System.out.println("Create NBA team of Spurs");
NBATeam spurs = new NBATeam("Spurs");
System.out.println("Add a player to team Spurs? (yes/no)");
ifAddPlayer = input.next();
while(ifAddPlayer.equalsIgnoreCase("yes")) {
System.out.println("Enter the name of the player: ");
spurs.addPlayer(playerName);
playerName = input.next();
System.out.println("Add another player to Spurs?");
ifAddPlayer = input.next();
}
playAGame(heats,spurs);
System.out.println(heats.toString());
System.out.println(spurs.toString());
}
public static void playAGame(NBATeam heats, NBATeam spurs) {
for(int game = 0; game < 7; game++){
double score = Math.random();
if (score < .5) {
heats.winAgame(spurs);
}
else{
spurs.winAgame(heats);
}
if(spurs.nWin()==4 || heats.nWin()==4) {
break;
}
}
if(spurs.nWin()>heats.nWin()) {
System.out.println("***Spurs Win!***");
}
else
System.out.println("***Heat Wins!***");
}
}
import java.util.Arrays;
public class NBATeam {
private String sTeamName;
private int nWin;
private int nLoss;
private int nPlayer;
private String [] playerArray = new String[nPlayer];
public NBATeam(String sTeamName) {
this.sTeamName = sTeamName;
nPlayer=0;
}
public int nWin() {
return nWin;
}
public int nLoss() {
return nLoss;
}
public String getTeamName(){
//gets team's Name
return sTeamName;
}
public String toString() {
return sTeamName + Arrays.toString(playerArray)+ "Wins: " + nWin +
" Loses: " + nLoss;
}
public void lossAgame() {
this.nLoss++;
}
public void winAgame(NBATeam team2) { //To win over the team
nWin++;
team2.setLoss(team2.nLoss()+1);
//teamB.lossAgame();
}
public int getLossNumber() {
return this.nLoss;
}
public void setLoss(int l) {
this.nLoss=l;
}
public int getWinNumber() {
return this.nWin;
}
public void setWin(int w) {
this.nWin=w;
}
public void addPlayer(String player) {
String temp[] = playerArray;
playerArray = Arrays.copyOf(temp, temp.length+1);
playerArray[nPlayer] = player;
nPlayer++;
}
public String[] getPlayerArray() {
return playerArray;
}
public void setPlayerArray(String[] playerArray) {
this.playerArray = playerArray;
}
}
You start with an initial value of null in playerName, and then you add the playerName before getting the value. This,
heats.addPlayer(playerName);
playerName = input.next();
is reversed. It should be
playerName = input.next();
heats.addPlayer(playerName);
And, you were consistent. This,
spurs.addPlayer(playerName);
playerName = input.next();
is likewise reversed. And should be
playerName = input.next();
spurs.addPlayer(playerName);
I would suggest you limit the visibility of your variables. If you had
String playerName = input.next();
heats.addPlayer(playerName);
it would limit the possibility of adding the player before you set it.
Related
so im very new to programming so sorry if is an inane question
But i have been given a homework where i have to create a transcript with java using array,
first i have to enter the students name and code, then the peogram should tell me to enter each lesson’s grade individually
And the result should display the student’s name,code and the grades i have entered for each lecture
I know how to enter make the program prompt me with each lecture and grade entry but showing all the grades i have entered next to thier lecture is what im stuck on
I would highly appreciate it if i get an answer.
Thanks
Take a look at this code, if there is something obscure, ask for clarification.
MainClass.java class
public class MainClass {
static boolean flag = true;
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
//create new student
Student student = new Student();
//print out enter name of a student
System.out.println("Enter name of student: ");
//read students name from next line
student.setNameOfStudent(keyboard.nextLine());
//print out enter code of a student
System.out.println("Enter code of student: ");
//read students password from a next line
student.setPasswordOfStudent(keyboard.nextLine());
//create a list of lectures to add them to a student
List<Lecture> lectures = new ArrayList<>();
//do adding lectures and grades for them while N is not pressed
do {
// enter new lecture name
System.out.println("Add a lecture? Y or N ");
//take answer for adding a lecture from a next input
String answerForAddingLecture = keyboard.next();
//if answer is yes
if ("Y".equalsIgnoreCase(answerForAddingLecture)) {
// print enter a name of a lecture
System.out.println("Enter a name of a lecture? ");
//take a lecture name from next input
String lectureName = keyboard.next();
// create a lecture
Lecture lecture = new Lecture();
// set lecture name from lectureName variable
lecture.setNameOfLecture(lectureName);
//create a list of grades
List<Grade> listOfGrades = new ArrayList<>();
//do inside brackets while flag is set to true
do {
//print Add a grade for this lecture? Y or N
System.out.println("Add a grade for this lecture? Y or N ");
//set an answer variable from the next input
String answer = keyboard.next();
//if answer is yes
if ("Y".equalsIgnoreCase(answer)) {
//print what grade is it going to be?
System.out.println("What grade is it going to be?");
//create new grade object
Grade grade = new Grade();
//set temp variable to next int input
int temp = keyboard.nextInt();
//set grade value to the value of temp variable
grade.setGrade(temp);
// add this grade to a list of grades
listOfGrades.add(grade);
}
//if answer is negative
else if ("N".equalsIgnoreCase(answer)) {
//set flag to false
flag = false;
}
}
//do above as long as flag is set to true
while (flag);
//reset flag to true again for keep adding new lectures
flag = true;
//set a list of grades to a lecture grades list
lecture.setGradesOfLecture(listOfGrades);
//set a lecture to a list of lectures
lectures.add(lecture);
//set a list of lectures to a students lecture list
student.setLecturesOfStudent(lectures);
}
//if answer is negative
else if ("N".equalsIgnoreCase(answerForAddingLecture)) {
flag = false;
}
} while (flag);
//print a student with all lectures and grades
System.out.println(student.toString());
}
}
Student.java class
public class Student {
private String nameOfStudent;
private String passwordOfStudent;
private List<Lecture> lecturesOfStudent;
public Student() {
}
public Student(String nameOfStudent, String passwordOfStudent, List<Lecture> lecturesOfStudent) {
this.nameOfStudent = nameOfStudent;
this.passwordOfStudent = passwordOfStudent;
this.lecturesOfStudent = lecturesOfStudent;
}
/**
* #return the nameOfStudent
*/
public String getNameOfStudent() {
return nameOfStudent;
}
/**
* #param nameOfStudent the nameOfStudent to set
*/
public void setNameOfStudent(String nameOfStudent) {
this.nameOfStudent = nameOfStudent;
}
/**
* #return the passwordOfStudent
*/
public String getPasswordOfStudent() {
return passwordOfStudent;
}
/**
* #param passwordOfStudent the passwordOfStudent to set
*/
public void setPasswordOfStudent(String passwordOfStudent) {
this.passwordOfStudent = passwordOfStudent;
}
/**
* #return the lecturesOfStudent
*/
public List<Lecture> getLecturesOfStudent() {
return lecturesOfStudent;
}
/**
* #param lecturesOfStudent the lecturesOfStudent to set
*/
public void setLecturesOfStudent(List<Lecture> lecturesOfStudent) {
this.lecturesOfStudent = lecturesOfStudent;
}
public String getLecturesWithGrades() {
String temp = "\nLectures:";
for (Lecture l : getLecturesOfStudent()) {
temp += "\n" + l.getNameOfLecture();
temp += "\nGrades:\n";
for (Grade g : l.getGradesOfLecture()) {
temp += g.getGrade() + " ";
}
}
return temp;
}
#Override
public String toString() {
return "Student: " + getNameOfStudent() + " with a code: " + getPasswordOfStudent() + getLecturesWithGrades();
}
}
Lecture.java class
public class Lecture {
private String nameOfLecture;
private List<Grade> gradesOfLecture;
public Lecture() {
}
public Lecture(String nameOfLecture, List<Grade> gradesOfLecture) {
this.nameOfLecture = nameOfLecture;
this.gradesOfLecture = gradesOfLecture;
}
public String getNameOfLecture() {
return nameOfLecture;
}
public List<Grade> getGradesOfLecture() {
return gradesOfLecture;
}
public void setNameOfLecture(String nameOfLecture) {
this.nameOfLecture = nameOfLecture;
}
public void setGradesOfLecture(List<Grade> gradesOfLecture) {
this.gradesOfLecture = gradesOfLecture;
}
#Override
public String toString() {
return getNameOfLecture() + " " + getGradesOfLecture();
}
}
Grade.java class
/**
* Might add remarks on grades or any other properties some time later, therefore new
* object is created called Grade.
* #author
*/
public class Grade {
int grade;
public Grade() {
}
public Grade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
#Override
public String toString() {
return getGrade() + " ";
}
}
I am trying to clean my code up by creating a class specifically for an array of information. It is basically like a storage for variables in case I need them later. Here is what I have so far:
package com.input;
import java.util.Scanner;
public class Gender extends Welcome {
Scanner input = new Scanner(System.in);
private static String gender;
public static void setGender() {
Scanner input = new Scanner(System.in);
int[] storeInts = new int[25];
storeInts[0] = 0;
//The [0] index of array storeInformation is the gender value. 0 = female; 1 = male
gender = input.nextLine();
if(gender.equalsIgnoreCase("boy")) {
System.out.println("What is your name, sir?");
while (storeInts[0] < 1) {
storeInts[0]++;
}
}else if(gender.equalsIgnoreCase("girl")) {
System.out.println("What is your name, ma'am?");
}else{
System.out.println("You have failed to answer correctly. Try again:");
init();
}
Name nameObject = new Name();
nameObject.setName(storeInts[0]);
}
public static void nextName(int x) {
if(x == 1) {
System.out.println("What is your name, sir?");
}else{
System.out.println("What is your name, ma'am?");
}
Name nameObject = new Name();
nameObject.setName2();
}
}
What I'm trying to accomplish here, is if the user types "boy" my code will store 1 in the index [0] of array storeInts[]. If the user types "girl" the index [0] will remain the value of 0.
If I need to refer to the user's gender later on, I want to be able to go back and figure out if they are a "boy" or a "girl" using the array.
I want to be able to called this array from any method within my code. I have already used this array in a complicated way and I would like to find a solution to make it easier.
Here is when I used it:
nameObject.setName(storeInts[0]);
I transferred the index [0] to the setName() method.
Here is the setName() method:
public void setName(int x) {
String name;
name = input.nextLine();
String storedStrings[] = new String[25];
storedStrings[0] = name;
FirstTask firstTaskObject = new FirstTask();
if (name.length() == 0) {
System.out.println("You must be a unicorn. You want to play games?");
altInit(x);
}else{
System.out.println("Nice to meet you, " + name + "!");
firstTaskObject.beginning(name);
}
}
As you can see I created another array in the same manner as the previous one, but this one is to store Strings instead. Now back to what I was saying-- the parameter (int x) is the same value as storeInts[0]. This will tell me if the user is male or female. This value is sent to altInit() method when the user decides to try to continue without typing their name in first.
Here is the altInit() method:
public void altInit(int x) {
String yesOrNo;
AltStory altStoryObject = new AltStory();
Gender backToGender = new Gender();
yesOrNo = input.nextLine();
if(yesOrNo.equalsIgnoreCase("yes")) {
altStoryObject.AltInit();
}else if(yesOrNo.equalsIgnoreCase("no")) {
System.out.println("Consider this your last warning...");
backToGender.nextName(x);
}else{
System.out.println("You have failed to answer correctly. Try again:");
init();
}
}
When asked if they want to play games, they can type "yes" or "no." If the user types "no" as in they do not want to play games, then the program will print, "Consider this your last warning..." and then continue to the nextName() method in the previous class Gender. This also passes on that index[0] again in the array storedInts[].
Here is the nextName() method:
public static void nextName(int x) {
if(x == 1) {
System.out.println("What is your name, sir?");
}else{
System.out.println("What is your name, ma'am?");
}
Name nameObject = new Name();
nameObject.setName2();
}
As you can see, if the user is that value of a male (or 1) then the program will print, "What is your name, sir?." If the value is a female (or 0), then the program will print, "What is your name, ma'am?"
This whole time I felt like the stored value of storeInts[0], was just leap frogging around until it was used... I want to prevent this by just creating a class with methods giving me the ability to call any value stored in that array whenever I need it. How do I create an array, store it in a method, and call it when needed?
As someone has requested, here is the entire code:
//Gender class
package com.input;
import java.util.Scanner;
public class Gender extends Welcome {
Scanner input = new Scanner(System.in);
private static String gender;
public void setGender() {
Scanner input = new Scanner(System.in);
int [] storeInts = new int[25];
storeInts[0] = 0;
//The [0] index of array storeInformation is the gender value. 0 = female; 1 = male
gender = input.nextLine();
if (gender.equalsIgnoreCase("boy")) {
System.out.println("What is your name, sir?");
while(storeInts[0]<1){
storeInts[0]++;
}
} else if (gender.equalsIgnoreCase("girl")) {
System.out.println("What is your name, ma'am?");
} else {
System.out.println("You have failed to answer correctly. Try again:");
init();
}
Name nameObject = new Name();
nameObject.setName(storeInts[0]);
}
public void nextName(int x){
if (x == 1) {
System.out.println("What is your name, sir?");
}else {
System.out.println("What is your name, ma'am?");
}
Name nameObject = new Name();
nameObject.setName2();
}
}
//Name class
package com.input;
public class Name extends Gender{
public void setName(int x) {
String name;
name = input.nextLine();
String storedStrings[] = new String[25];
storedStrings[0] = name;
FirstTask firstTaskObject = new FirstTask();
if (name.length() == 0) {
System.out.println("You must be a unicorn. You want to play games?");
altInit(x);
} else {
System.out.println("Nice to meet you, " + name + "!");
firstTaskObject.beginning(name);
}
}
public void altInit(int x){
String yesOrNo;
AltStory altStoryObject = new AltStory();
Gender backToGender = new Gender();
yesOrNo = input.nextLine();
if(yesOrNo.equalsIgnoreCase("yes")) {
altStoryObject.AltInit();
}else if(yesOrNo.equalsIgnoreCase("no")){
System.out.println("Consider this your last warning...");
backToGender.nextName(x);
}else{
System.out.println("You have failed to answer correctly. Try again:");
init();
}
}
public void setName2() {
String name;
name = input.nextLine();
FirstTask firstTaskObject = new FirstTask();
if (name.length() == 0) {
System.out.println("You have failed to answer correctly. Try again:");
init();
} else {
System.out.println("Nice to meet you, " + name + "!");
firstTaskObject.beginning(name);
}
}
}
How do I create an array, store it in a method, and call it when needed?
public class Coach extends Team {
private int Code;
public Coach(String name, String team, int age) {
super(name, team, age);
}
public Coach() {
}
public void setCode(int code) {
this.Code = code;
}
public int getCode() {
return Code;
}
public String code[] = {
"qwe",
"asd",
"zxc"
};
public String CoachName[] = {
"person1",
"person2",
"person3"
};
}
The main class
import com.org.FS.Coach;
import com.org.FS.Team;
import java.util.Scanner;
public class TeamTest {
public static void main(String[] args) {
System.out.println("Welcome to Federation System!! \n\nPlease enter your name:");
Scanner input = new Scanner(System.in);
String name = input.nextLine();
System.out.println("\nHi " + name + " !!\n\nPlease select an option\n 1- Guest and 2- Admin : \n Guest Admin ");
int choice = input.nextInt();
System.out.println("You choose --- " + choice);
switch (choice) {
case 1:
System.out.println("Table Info");
System.out.println("=============================================================");
break;
case 2:
System.out.println(" ==Log in== ");
}
if (choice == 1) {
Card c = new Card();
System.out.println(" ==Players==\n" + c);
System.out.println("==============================================================");
} else if (choice == 2) {
Coach co=new Coach();
System.out.println("Enter username:\n");
name=input.nextLine();
System.out.println("\n\nWelcome " + name + "\nPlease enter your verification code :");
String code1=input.nextLine();
for(int i=0;i<co.CoachName.length;i++) {
if( name.equals(co.CoachName[i]) || code1.equals(co.code[i]) && (!co.getName().equals(co.CoachName[++i]) || !co.code.equals(co.code[++i])) )
System.out.println("Wrong code!\nTry Again!");
else
System.out.println("Success !");
}
}
}
}
my project contains 5 classes named
TeamTest1(the main class)
Team
Players
Coach
and Card
the team class is the superclass and have coach and players as subclasses.
When i run the code and i press 1 the guest it shows only the players information.If i press two Admin,i want to put the name person1 like i have it in the array and the password too.Im very confused if i need to creATE A new array or to use the one i have...
for getting user input repeatedly you can use a while loop like so
int choice = input.nextInt();
while (choice != 3) {
...
choice = input.nextInt();
}
for getting (user,password) pairs you can use a map like so
Map users = new HashMap();
m.put("person1", "qwe");
m.put("person2", "asd");
m.put("person3", "zxc");
and check if the key value match
value = m.get(co.CoachName)
if code.equals(value) ....
Don't forget to use try catch and/or check for integrity of user input and keys exists in map and so...
I am trying to implement an Inventory program for shoes. What I am trying to do is give each shoe an id (productId) and a quantity of shoes that are in stock (ammountToPick). I want to be able to ask the user running the program to enter a shoe id to get the amount of that type of shoe that are left in stock (ammountToPick). My current problem with my program is that it is not returning anything and just keeps printing "invalid product id".
I have provided my code below:
public class Product {
private String productId = "";
private int ammountToPick = 0;
private int ammountToRestock = 0;
public Product (String productId, int ammountToPick){
this.productId = productId;
this.ammountToPick = ammountToPick;
}
public String getProductId() {
return productId;
}
public int getAmmountToPick() {
return ammountToPick;
}
}
public class Shoes extends Product{
public Shoes(String productId, int ammountToPick){
super(productId, ammountToPick);
}
}
import java.util.Scanner;
public class Inventory
{
private static String productId = "";
private int ammountToPick = 0;
private int ammountToRestock = 0;
public static final int MAX_ITEMS = 999999;
private static Product product [] = new Shoes[MAX_ITEMS];
public static void main (String args[]){
buildInventory();
getInventory();
}
public static void buildInventory(){
product[1] = new Shoes("shoe101", 19);
product[2] = new Shoes("shoe200", 1);
product[3] = new Shoes("shoe420", 9);
}
public static void getInventory() {
Scanner input = new Scanner(System.in);
System.out.println("Enter the product id of the product you would like to pick: ");
String userinput = input.nextLine();
if(userinput.equals(productId)) {
System.out.println("there are" + product[1].getAmmountToPick() +"left");
}
else {
System.out.println("invalid product id ");
}
}
}
if(userinput.equals(productId)) {
System.out.println("there are" + product[1].getAmmountToPick() +"left");
}
else {
On the first line, you problem is that productId is set to an empty string at the top of the class. It should actually work if you just hit enter without entering an actual productId and userinput is "". But to make this work the way you want, get rid of your productId variable and check if userinput matches a productId of one of the items in your array
you need to do
userinput = ...; //this part is fine
for (Product p : products) {
if (userinput.equals(p.getProductId())) {
System.out.println('there are ' + p.getAmmountToPick() + " left");
}
}
Both the Product and Inventory classes have productId member variables. You're comparing userInput to the private static productIdin the Inventory class, which is an empty string.
I think you should be iterating through your inventory array looking for a match to the productId of a Product.
Here's one way to do it with an enhanced loop and no new methods:
boolean found = false;
for (final Product aProduct: product) {
if(userInput.equals(aProduct.getProductId())) {
System.out.println("there are" + aProduct.getAmmountToPick() +"left");
found = true;
break;
}
}
if(!found) {
System.out.println("invalid product id ");
}
I'd rename the product member variable to products so its intent is clearer. It would make the code above clearer as well.
Inventory, in the way you're using it, needs no productId. It should be deleted.
newbie here at Java. I'm working on a project to where I collect info from a user for two types of Creatures. The menu would look like:
1. Land based
2. Water based
3. Display Animal
4. Quit.
I'm wanting to collect the user information from the user and then display it back to them. I think I'm stuck on building the object constructors from the different object.
Here's what I have so far:
SuperClass:
package com.animal;
public class Creature {
private String size;
private String weight;
public Creature (String size, String weight){
this.size = size;
this.weight = weight;
}
public String getSize() {return size;}
public void setSize(String size) {this.size = size;}
public String getWeight() {return weight; }
public void setWeight(String weight) {this.weight = weight; }
void displayCr(){
System.out.println("***Creatures***");
System.out.println("Size: " + size);
System.out.println("Weight: " + weight);
This is my Land Subclass:
package com.animal;
public class Land extends Creature {
private String landAnimal;
public String getLandAnimal() {return landAnimal;}
public void setLandAnimal(String landAnimal) {this.landAnimal = landAnimal;}
public Land(String size, String weight, String landAnimal) {
super(size, weight);
this.landAnimal = landAnimal;
This is my Water subclass:
package com.animal;
public class Water extends Creature {
private String fish;
public String getFish() {return fish;}
public void setFish(String fish) {this.fish = fish;}
public Water(String size, String weight, String fish) {
super(size, weight);
this.fish = fish;
}
Then this is my Main called Kingdom:
package com.animal;
import java.util.ArrayList;
import java.util.Scanner;
public class Kingdom {
public static void main(String[] args) {
ArrayList<Creature> user = new ArrayList<Creature>();
Scanner input = new Scanner(System.in);
int selection = 0;
while(selection != 4){
System.out.println("****Main Menu****");
System.out.println("Enter 1 for Land Animal");
System.out.println("Enter 2 for Water Animal");
System.out.println("Enter 3 to Display Animal");
System.out.println("Enter 4 to quit");
selection = input.nextInt();
if(selection==1 || selection==2){
Creature userInfo = null;
System.out.println("Enter Size ");
String size = input.next();
System.out.println("Enter Weight: ");
String weight = input.next();
}
if(selection == 1){
System.out.println("Enter Land animal type: ");
String landAnimal = input.next();
//userInfo = new Land(size, weight, landAnimal);
//user.add(userInfo);
}
else if(selection == 2){
System.out.println("Enter Water animal type: ");
String fish = input.next();
//userInfo = new Water(size, weight, fish);
}
//creature.add(userInfo);
//System.out.println(user.displayCr());
}
I feel like I'm on the right path, but the last steps just aren't clicking for me and I've been grinding on this, reading, videos and nothing is clicking.
Also, I apologize if I've made the newbie mistakes in this post. I will accept all criticism, suggestions and help as a positive lesson. Thanks.
Overall your code is fairly right.
Except:
Creature userInfo = null; you are defining it inside the first IF, its scope will be limited to that IF.
As soon as you leave that IF it ceases to exist, hence you can't use it in the following IFs.
Consider the following scope change:
if(selection==1 || selection==2){ // main IF
Creature userInfo = null;
...
if(selection == 1){
...
}
else {
...
}
System.out.println(userInfo.displayCr());
} // end of main IF
First of all expand the scope of size,weight and userInfo means keep it inside main but outside the conditions as it is being used everywhere. Something like this.
int selection = 0;
String size = null;
String weight = null;
Creature userInfo = null;
Instead of this
creature.add(userInfo); should be
user.add(userInfo);
and call display like this
userInfo.displayCr(); because the return type of the method is void so cannot be used inside sysout and it should be inside that else if(){}