I'm trying to get a program I made to ask someone for a number between 1 and 13 in order to get them to make a selection. I'm trying to figure out how to handle if they want to be contrarian and enter a non-valid number or a character or string. Here's what I have so far...
try {
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
} catch (NullPointerException e) {
System.out.println("Invalid option, please pick a valid option\n");
showUnitSelection();
attackingUnit = selectUnit(input.nextInt());
attackerUnitName = attackingUnit.getUnitName();
}
Here's the method I'm using for a making the selection itself:
private static Unit selectUnit(int selection) {
switch (selection) {
case 1:
return Unit.GreatSwords;
case 2:
return Unit.BlackOrcs;
case 3:
return Unit.Bestigor;
case 4:
return Unit.ChaosChosen;
case 5:
return Unit.MenAtArms;
case 6:
return Unit.Executioners;
case 7:
return Unit.GraveGuard;
case 8:
return Unit.Retributors;
case 9:
return Unit.StormVermin;
case 10:
return Unit.SwordMasters;
case 11:
return Unit.TombGuard;
case 12:
return Unit.WildWoodRangers;
case 13:
return Unit.Hammerers;
}
return null;
}
I'm pretty sure I'm not doing this right, if you don't mind I'd like to hear some suggestions to consider.