Reputation:
I'm trying to create a game character, and the user is asked to input a number between 0-18. I input 19 to see if the code will tell me to try again, but it just ignores it and then prints out the character. It also does the same thing for the magic amount. The user should enter between 0 & 50, but if I enter a number higher than 50, it will also still print it. I want the code to tell me the input was wrong and try again. Is there a way I could ask the user to try again without using an exception?
public class Character {
private String name;
private int strength;
public Character() {
name = "TBD";
strength = 15;
}
public Character(String name, int strength) {
this.name = name;
this.strength = strength;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
int count = 1;
while (true) {
if (strength >= 0 && strength <= 18) {
System.out.println("Value is out of range 0-18");
System.out.println("Please try again");
break;
}
else {
this.strength = strength;
count = 0;
}
}
}
public String toString() {
return "Name: " + name + ", Strength: " + strength;
}
}
public class Human extends Character {
private String name;
private int strength;
private String weapon;
private int magicAmount;
public Human(String name, int strength, String weapon, int magicAmount) {
this.name = name;
this.strength = strength;
this.weapon = weapon;
this.magicAmount = magicAmount;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public String getWeapon() {
return weapon;
}
public void setWeapon(String weapon) {
this.weapon = weapon;
}
public int getMagicAmount() {
return magicAmount;
}
public void setMagicAmount(int magicAmount) {
int count = 1;
while (count != 0) {
if ((magicAmount >= 0 && magicAmount <= 50)) {
System.out.println("Value is out of range 0-50");
System.out.println("Please try again");
break;
}
else {
this.magicAmount = magicAmount;
count = 0;
}
}
}
public String toString() {
return "Human [Name: " + name + ", Strength: " + strength + ", Weapon: "
+ weapon + ", Magic Amount: " + magicAmount + "]";
}
}
ArrayList<Character> characters = new ArrayList<Character>();
String keepLooping = "y";
do {
System.out.println("Which character would you like to create (Human, Robot, or Animal): ");
String choice = input.nextLine();
if (choice.equalsIgnoreCase("Human")) {
System.out.println("Enter a name for the Human: ");
String name = input.next();
System.out.println("Enter the amount of stength for Human (0-18): ");
int strength = input.nextInt();
input.nextLine();
System.out.println("Enter a weapon for Human (Sword or Dagger): ");
String weapon = input.nextLine();
System.out.println("Enter the magic amount for your selected weapon (0-50): ");
int magicAmount = input.nextInt();
Human charH = new Human(name, strength, weapon, magicAmount);
characters.add(charH);
}
input.nextLine();
System.out.println("Would you like to create more characters - y or n");
keepLooping = input.nextLine();
}
while (keepLooping.equalsIgnoreCase("y"));
for (int i = 0; i < characters.size(); i++) {
Character c = characters.get(i);
System.out.println(c);
}
}
Upvotes: 1
Views: 59
Reputation: 1761
There is several issues with your code regarding the creation of a Human.
Problem 1
First problem is that you create Human with an all args constructor. That way the setStrength(...) and setMagicAmount(...) is never called.
Solution 1
Make use of the setters. Note this will still not work. See problem 2 and 3.
public Human(String name, int strength, String weapon, int magicAmount) {
this.name = name;
//this.strength = strength;
setStrength(strength); // not this will still not work! Problem 2 and 3
this.weapon = weapon;
//this.magicAmount = magicAmount;
setMagicAmount(magicAmount); // Will still not work. See Problem 3
}
Solution 2
Instead of having an all args constructor you can have a no args constructor and then call the setters. Will still not work. Se problem 2 and 3.
Human human = new Human();
human.setName(name);
human.setStrength(strength); // will not work. See problem 2 and 3
human.setWeapon(weapon);
human.setMagicAmount(magicAmount); // will not work. See problem 3
Problem 2
Class Human extends Character but still you are overriding the setStrength(...) method inside human. This method does not have any rules.
Delete this method inside Human and the correct setStrength(...) from Character with the rules will be called because it is inherited. This will still not work. See problem 4.
Problem 3
You have rules inside setStrength(...) and setMagicAmount(...) with range checks. The problem is that the range checks for both methods is wrong and you do not need the loop.
Solution
First check the value. If outside the range tell the user, else set the value.
For setStrength(...) change to
public void setStrength(int strength) {
if (strength < 0 || strength > 18) { // less than 0 or more than 18
throw new IllegalArgumentException("Value is out of range 0-18");
}
this.strength = strength;
}
For setMagicAmount(...) change to
public void setMagicAmount(int magicAmount) {
if (magicAmount < 0 || magicAmount > 50) { // less than 0 or more than 50
throw new IllegalArgumentException("Value is out of range 0-50");
}
this.magicAmount = magicAmount;
}
Upvotes: 0
Reputation: 7404
You should do validation on the user input, for example (strength):
Integer strength = null;
while (strength == null) {
System.out.println("Enter the amount of stength for Human (0-18): ");
strength = input.nextInt();
if (strength < 0 || strength > 18) {
strength = null;
System.out.println("Invalid strength, please try again");
}
}
The other issue is that you have your validation login in setStrength
, but that is never called, as you set the strength in the constructor. So a fix would be:
public Character(String name, int strength) {
setName(name);
setStrength(strength);
}
Upvotes: 0