0

I'm pretty new to Java. I'm making a class for a card. The face is the number of the card, and can only be 1-13. I'm trying to validate it by setting f to 1-13. I wanted to simply do this:

public void setFace(int f)
   {

    f int >= 1 && f <= 13);
   }

Obviously, you can't do that. How do would I simply set f to the numbers 1-13? Thanks for any help!

2
  • this will not compile either Commented Apr 27, 2014 at 7:04
  • You're going about validation the wrong way. You should check whether the number is in the valid range and do something if it isn't, not set it equal to all the valid numbers. Commented Apr 27, 2014 at 7:09

2 Answers 2

1

I would do in this way

public void setFace(int f) {

if(f < 1 || f > 13)
{
   throw new IllegalArgumentException("Invalid face value "+f+" face must be between 1-13");
}
}

Angelo

Sign up to request clarification or add additional context in comments.

Comments

0
if (f >= 1 && f <= 13) {
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.