I am attempting to create a method by which I can return the value of an enum set by the users input. I decalred an enum, then I try to set the enum by the users input. I am unable to use my GetPlayerClass method to return the value of Player_Class. Can someone please tell me if this is the correct way to use an enum to set a value or is there a way I can allow the user to select from the enum list and let me get their choice.
My code is below.
TLDR: I am "enum dumb" and can't figure out how to set the value of a variable from an enum and then return that value later. Please view my code and let me know what I am doing wrong.
// The PCs base class
private enum Base_Class {
Barbarian, Bard, Cleric, Druid, Fighter, Monk,
Paladin, Ranger, Rogue, Sorcerer, Wizard
};
// Setting the base class
public void setBaseClass() {
do {
System.out.println("Available Classes: ");
System.out.println("Please select a cooresponding number for class.");
int i = 1;
for(Base_Class value: Base_Class.values()){
System.out.println(i+": "+value.name());
i++;
}
try {
Base_Class_Choice = user_input.nextInt();
switch(Base_Class_Choice) {
case 1:
Base_Class Player_Class;
Player_Class = Base_Class.Barbarian;
break;
case 2:
break;
default:
break;
}
} catch (Exception e) {
System.out.println("You must choose a valid class. Try numbers.");
user_input.next();
}
} while (Base_Class_Choice == 0);
}
/**
* Return my players class
* @return
*/
public String getPlayerClass() {
return Player_Class;
}
I updated the code below My try area now looks like so.
try {
Base_Class_Choice = user_input.nextInt();
Base_Class Player_Class = Base_Class.values()[Base_Class_Choice - 1];
} catch (Exception e) {
System.out.println("You must choose a valid class. Try numbers.");
user_input.next();
}
But the return for the Base_Class Player_Class does not work.
public String getPlayerClass() {
return Player_Class;
}
When I try to return Player_Class it still fails out with the error cannot find symbol. What could I still be doing wrong?
UPDATE!!!! "ALL THE CODE!!!"
/*
*
*
*/
package rpgmanager;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
/**
*
* @author Aaron
*/
public class Character {
// The person playing the character
private String Player_Name;
// The PCs First Name
private String First_Name;
// The PCs Last Name
private String Last_Name;
// The PCs Race
private enum Race {
Dwarf, Halfling, Elf, Human, Gnome, HalfOrc, HalfElf
};
// The PCs base class
private enum Base_Class {
Barbarian, Bard, Cleric, Druid, Fighter, Monk,
Paladin, Ranger, Rogue, Sorcerer, Wizard
};
private Base_Class Player_Class;
// Base Class Choice for switch case
private int Base_Class_Choice = 0;
// The PCs Height
private int Height = 0;
// The PCs Weight
private int Weight = 0;
// The PCs Age
private int Age = 0;
// The PCs base level
private int Base_Level;
// Sets up the scanner for inputs
Scanner user_input = new Scanner(System.in);
// Create a Buffered reader for input
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// This instantiates a new character
public Character() {
}
/**
* Sets the PCs first name
*/
public void setPlayerName() {
System.out.print("Players Name: ");
try {
Player_Name = reader.readLine();
} catch (IOException ioe) {
System.out.println("The Player Name input failed to set.");
System.out.println(ioe);
}
}
/**
* Sets the PCs first name
*/
public void setFirstName() {
System.out.print("First Name: ");
try {
First_Name = reader.readLine();
} catch (IOException ioe) {
System.out.println("The PCs First Name input failed to set.");
System.out.println(ioe);
}
}
/**
* Sets the PCs last name
*/
public void setLastName() {
System.out.print("Last Name: ");
try {
Last_Name = reader.readLine();
} catch (IOException ioe) {
System.out.println("The PCs Last Name input failed to set.");
System.out.println(ioe);
}
}
/**
* Sets the height of the PC
*/
public void setHeight() {
do {
System.out.print("Height (inches): ");
try {
Height = user_input.nextInt();
if(Height < 1) {
System.out.println("Not a valid number.");
}
} catch(Exception e) {
System.out.println("You must use a number greater than 0!");
user_input.next();
}
} while (Height < 1);
}
/**
* Sets the weight of the PC
*/
public void setWeight() {
do {
System.out.print("Weight (pounds): ");
try {
Weight = user_input.nextInt();
if (Weight < 1) {
System.out.println("Not a valid number.");
}
} catch (Exception e) {
System.out.println("You must use a number greater than 0!");
user_input.next();
}
} while (Weight < 1);
}
/**
* Sets the age of the PC
*/
public void setAge() {
do {
System.out.print("Age (whole years): ");
try {
Age = user_input.nextInt();
if (Age < 1) {
System.out.println("Not a valid number.");
}
} catch (Exception e) {
System.out.println("You must use a number greater than 0!");
user_input.next();
}
} while (Age < 1);
}
/**
* Sets the base level of the PC
*/
public void setBaseLevel() {
do {
System.out.print("Starting Level: ");
try {
Base_Level = user_input.nextInt();
if (Base_Level < 1 || Base_Level > 25) {
System.out.println("Not a valid number.");
}
} catch (Exception e) {
System.out.println("You must choose a valid level between 1 and 25!");
user_input.next();
}
} while (Base_Level < 1 || Base_Level > 25);
}
public void setBaseClass() {
do {
System.out.println("Available Classes: ");
System.out.println("Please select a cooresponding number for class.");
int i = 1;
for(Base_Class value: Base_Class.values()){
System.out.println(i+": "+value.name());
i++;
}
try {
Base_Class_Choice = user_input.nextInt();
Base_Class Player_Class = Base_Class.values()[Base_Class_Choice - 1];
} catch (Exception e) {
System.out.println("You must choose a valid class. Try numbers.");
user_input.next();
}
} while (Base_Class_Choice == 0);
}
/**
* Gets the PCs first name
* @return
*/
public String getFirstName() {
return First_Name;
}
/**
* Gets the PCs last name
* @return
*/
public String getLastName() {
return Last_Name;
}
/**
* Gets the PCs height
* @return
*/
public int getHeight() {
return Height;
}
/**
* Gets the PCs age
* @return
*/
public int getAge() {
return Age;
}
/**
* Gets the PCs base level (1-25)
* @return
*/
public int getBaseLevel() {
return Base_Level;
}
/**
* Gets the PCs player name
* @return
*/
public String getPlayerName() {
return Player_Name;
}
/**
*
* @return
*/
public Base_Class getPlayerClass() {
return Player_Class;
}
/**
* Creates a new character
*/
public void createCharacterNew() {
this.setPlayerName();
this.setFirstName();
this.setLastName();
this.setHeight();
this.setWeight();
this.setAge();
this.setBaseLevel();
this.setBaseClass();
}
}