So lets say I have this enum
public enum StatType
{
Strength, Dexterity, Intelligence, Wisdom, Luck
}
and I have this function,
public void SetStats(StatType type, int value)
{
switch (type)
{
case StatType.Strength:
hero.Stats.Strength += value;
break;
case StatType.Dexterity:
hero.Stats.Dexterity += value;
break;
case StatType.Intelligence:
hero.Stats.Intelligence += value;
break;
case StatType.Wisdom:
hero.Stats.Wisdom += value;
break;
case StatType.Luck:
hero.Stats.Luck += value;
break;
}
}
How can I change this incoming stat's "type" parameter to hero.Stats.( ) variable's name?
If this is possible, then I don't need to use switch, right?
Related
I have this method:
void schedule(){
switch (schedule.getType()) {
case MANUAL:
scheduleManual();
break;
case AUTO:
scheduleAuto();
break;
case NONE:
scheduleNone();
break;
default:
break;
}
}
And I'd like to know if there's a better design to have this instead of a switch.
void schedule(){
def method = 'schedule' + schedule.getType().toString().toLowerCase().capitalize()
this."$method"()
}
You could also use methodMissing() magic:
enum Type {MANUAL, AUTO, NONE}
schedule = Type.AUTO
def methodMissing(String name, args){
if( name in Type.values()*.toString() )
this."schedule${name.toLowerCase().capitalize()}"()
}
void schedule(){
this."$schedule"()
}
void scheduleManual(){ println "calling scheduleManual" }
void scheduleAuto(){ println "calling scheduleAuto" }
schedule()
I am developing an automation testing code with the following error:
Type mismatch: cannot convert from CellType to int.
Please what can I do?
public static String cellToString(HSSFCell cell) {
// TODO Auto-generated method stub
int type;
Object result;
type = cell.getCellType();
switch (type) {
case 0 : // numeric value in Excel
result = cell.getNumericCellValue();
break;
case 1 : //String value in Excel
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("No support for this of cell");
}
return result.toString();
}
CellType is an enum, not an int. The type of your type variable should be CellType and your switch should look like this:
CellType type = cell.getCellType();
switch (type) {
case CellType.NUMERIC : // numeric value in Excel
result = cell.getNumericCellValue();
break;
case CellType.STRING : //String value in Excel
result = cell.getStringCellValue();
break;
default:
throw new RuntimeException("No support for this of cell");
}
Alternatively, you can use Enum#ordinal(), which returns an ordinal integer of the enum value, but the example above is much preferable.
EDIT: Also have a look at this answer about how to get cell value as string using Formatter instead of switch.
I am trying to override the default value of enum passed as a parameter, but I am getting the default value.
class MyClass
{
public:
enum MyCalculation
{
SUM,
DIVIDE,
MULTIPLY,
NOCALCULATION
};
void foo(MyCalculation cal = NOCALCULATION);
void DoCalculation();
};
void MyClass::DoCalculation()
{
MyCalculation cal;
// here I am getting the enum value at runtime
int val = // get enum value
switch(val)
{
case 0:
cal = MyClass::SUM;
break;
case 1:
cal = MyClass::DIVIDE;
break;
case 2:
cal = MyClass::MULTIPLY;
break;
}
foo(cal);
}
void MyClass::foo(MyCalculation getCal)
{
cout << getCal;
}
I tried passing by reference in DoCalculation method, but I am always getting NOCALCULATION in foo.
Do I need to do something else?
I'm having a bit of trouble getting a few parts of my code to work properly.
I'm still a bit new to java and could some direction and clues to where I went wrong.
The error comes from the if statements. I feel like i know why they are erring out because the || are undefined but I'm not sure how to fix it. What I'm trying to get it to do is take the inputs either L,R,F,B (left, right, forward and back). lowercase the input and either accept either one or the other using boolean "or".
import java.util.Scanner;
public class ChooseYourAdventure {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Choose a diection: ");
String direction = input.nextLine().toLowerCase();
System.out.printf(" %s and %s/n",getDirection (way),getYourChoice (found));
}
public static String getYourChoice (String found) {
String result = "Unknown";
switch (found)
{
case "l":
result = " now we all know you can turn left unlike Zoolander";
break;
case "left":
result = " now we all know you can turn left unlike Zoolander";
break;
case "r":
result = " you fall down a hole never to be seen again... sad.";
break;
case "right":
result = " you fall down a hole never to be seen again... sad.";
break;
case "f":
result = " YOU ARE THE KWISATZ HADERACH!!";
break;
case "forward":
result = " YOU ARE THE KWISATZ HADERACH!!";
break;
case "b":
result = " you are a scaredy cat but, you live to fight or runaway another day";
break;
case "back":
result = " you are a scaredy cat but, you live to fight or runaway another day";
break;
}
return result;
}
public static String getDirection(String way) {
String result;
if (way == "l" || "left") {
System.out.print("Your character moves left");
}
else if (way == "r" || "right") {
System.out.println("You character moves right");
}
else if (way == "f" || "forward") {
System.out.println("Your character moves forward");
}
else if (way == "b" || "back") {
System.out.println("Your character moves forward");
}
else {
System.out.println(" You cant go that way ");
}
return result;
}
}
All your if statements are wrong. When using || or &&, you need to specify the variable way on each side of the ||:
if (way == "l" || way == "left") {
System.out.print("Your character moves left");
}
I want to know weather is is possible or not, a way to call different classes on the basis of an integer value without conditional statements like switch case?
What I am having is:
int val = // getting some value here from a method
String data = // getting some value here from a method
switch(val)
{
case 1:
{
new TempClass1(data);
break;
}
case 2:
{
new TempClass2(data);
break;
}
}
What I want is like:
int val = // getting some value here from a method
String data = // getting some value here from a method
new TempClass(val, data);
This should call the object of TempClass1 or TempClass1 as per "val"
Any help will be appreciated.
Maybe use a Factory for the classes, assuming your two classes share a base class named TempBaseClass:
class TempClassFactory {
static public TempBaseClass getTempClass(int val, String data)
{
switch(val)
{
case 1:
{
return new TempClass1(data);
break;
}
case 2:
{
return new TempClass2(data);
break;
}
default:
throw new Exception("Bad value");
}
}
}
int val = // getting some value here from a method
String data = // getting some value here from a method
TempClassFactory::getTempClass(val, data);