simple IF question if (newplayer == goalkeeper) {throw error}

Z

zcraven

if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}


I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Thanks,
Zac
 
J

Joona I Palaste

zcraven <[email protected]> scribbled the following
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Because you have ten individual variables p2 to p11, the best you can do
is either a series of if statements:

if (p2 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
if (p3 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
/* and so on */

or one if statement with a large chained condition:

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper /* and so on
*/) {
throw new IllegalArgumentException("No way!");
}

Your code, however, simply *screams* out for an array. You could have:

Player[] p = new Player[11];
/* assign values to p[0] to p[10] */
if (p[0] instanceof OutfieldPlayer) {
throw new IllegalArgumentException("Goalkeepers can't play " +
"outfield");
}
for (int i=1; i<11; i++) {
if (p instanceof Goalkeeper) {
throw new IllegalArgumentException("Outfield players can't " +
"keep goals");
}
}

This way you could accommodate teams of several hundred or thousand
players without having to retype the same code over and over again.
 
T

Thomas Schodt

zcraven said:
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}


I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

You should probably have a moveTo() method in Player
and override it as appropriate so
newplayer.moveTo(OUTFIELD)
would throw an exception if newplayer is an instance of Goalkeeper.
 
Z

zcraven

Joona I Palaste said:
zcraven <[email protected]> scribbled the following
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Because you have ten individual variables p2 to p11, the best you can do
is either a series of if statements:

if (p2 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
if (p3 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
/* and so on */

or one if statement with a large chained condition:

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper /* and so on
*/) {
throw new IllegalArgumentException("No way!");
}

Your code, however, simply *screams* out for an array. You could have:

Player[] p = new Player[11];
/* assign values to p[0] to p[10] */
if (p[0] instanceof OutfieldPlayer) {
throw new IllegalArgumentException("Goalkeepers can't play " +
"outfield");
}
for (int i=1; i<11; i++) {
if (p instanceof Goalkeeper) {
throw new IllegalArgumentException("Outfield players can't " +
"keep goals");
}
}

This way you could accommodate teams of several hundred or thousand
players without having to retype the same code over and over again.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson



Thank you kind sir.

The clubFirst11 will always be of size 11 (hence the name), so I think I
will use the 2nd thing you suggested.

Is it possible to create an array of a fixed size (11) that behaves the same
as an arraylist (so I dont have to change my code too much)? Should I do
this?
 
Z

zcraven

Joona I Palaste said:
zcraven <[email protected]> scribbled the following
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Because you have ten individual variables p2 to p11, the best you can do
is either a series of if statements:

if (p2 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
if (p3 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
/* and so on */

or one if statement with a large chained condition:

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper /* and so on
*/) {
throw new IllegalArgumentException("No way!");
}

Your code, however, simply *screams* out for an array. You could have:

Player[] p = new Player[11];
/* assign values to p[0] to p[10] */
if (p[0] instanceof OutfieldPlayer) {
throw new IllegalArgumentException("Goalkeepers can't play " +
"outfield");
}
for (int i=1; i<11; i++) {
if (p instanceof Goalkeeper) {
throw new IllegalArgumentException("Outfield players can't " +
"keep goals");
}
}

This way you could accommodate teams of several hundred or thousand
players without having to retype the same code over and over again.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Shh! The maestro is decomposing!"
- Gary Larson




it doesnt compile ('inconvertible types')
 
Z

zcraven

public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3,
OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6,
OutfieldPlayer p7,
OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)

{
if (g1 instanceof OutfieldPlayer){
throw new IllegalArgumentException("You cannot put an outfield
player in goal");
}

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper || p4
instanceof Goalkeeper || p5 instanceof Goalkeeper ||
p6 instanceof Goalkeeper || p7 instanceof Goalkeeper || p8
instanceof Goalkeeper || p9 instanceof Goalkeeper ||
p10 instanceof Goalkeeper){
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
}
 
J

Joona I Palaste

zcraven <[email protected]> scribbled the following
Joona I Palaste said:
zcraven <[email protected]> scribbled the following
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Because you have ten individual variables p2 to p11, the best you can do
is either a series of if statements:

if (p2 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
if (p3 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
/* and so on */

or one if statement with a large chained condition:

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper /* and so on
*/) {
throw new IllegalArgumentException("No way!");
}

Your code, however, simply *screams* out for an array. You could have:

Player[] p = new Player[11];
/* assign values to p[0] to p[10] */
if (p[0] instanceof OutfieldPlayer) {
throw new IllegalArgumentException("Goalkeepers can't play " +
"outfield");
}
for (int i=1; i<11; i++) {
if (p instanceof Goalkeeper) {
throw new IllegalArgumentException("Outfield players can't " +
"keep goals");
}
}

This way you could accommodate teams of several hundred or thousand
players without having to retype the same code over and over again.

Thank you kind sir.
The clubFirst11 will always be of size 11 (hence the name), so I think I
will use the 2nd thing you suggested.
Is it possible to create an array of a fixed size (11) that behaves the same
as an arraylist (so I dont have to change my code too much)? Should I do
this?

Not really directly possible, but arrays and ArrayLists can be easily
converted back and forth.

To convert from an array to a List:
Player[] p = /* get players from somewhere */
List pl = Arrays.asList(p);

To convert a List to an array:
List pl = /* get players from somewhere */
Player[] p = (Player[])pl.toArray(new Player[0]);

Note the use of List instead of ArrayList. This is because
Arrays.asList() is not guaranteed to return an ArrayList, only some
kind of List, and anyway you very rarely need the specific behaviour
of an ArrayList that a List doesn't have. You still have to construct
your lists with new ArrayList() though. new List() won't work as List
is only an interface.
 
J

Joona I Palaste

zcraven <[email protected]> scribbled the following
Joona I Palaste said:
zcraven <[email protected]> scribbled the following
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
I want to check that the variables p2 to p11 are of class OutfieldPlayer and
not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Because you have ten individual variables p2 to p11, the best you can do
is either a series of if statements:

if (p2 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
if (p3 instanceof Goalkeeper) {
throw new IllegalArgumentException("No way!");
}
/* and so on */

or one if statement with a large chained condition:

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper /* and so on
*/) {
throw new IllegalArgumentException("No way!");
}

Your code, however, simply *screams* out for an array. You could have:

Player[] p = new Player[11];
/* assign values to p[0] to p[10] */
if (p[0] instanceof OutfieldPlayer) {
throw new IllegalArgumentException("Goalkeepers can't play " +
"outfield");
}
for (int i=1; i<11; i++) {
if (p instanceof Goalkeeper) {
throw new IllegalArgumentException("Outfield players can't " +
"keep goals");
}
}

This way you could accommodate teams of several hundred or thousand
players without having to retype the same code over and over again.

it doesnt compile ('inconvertible types')

You said yourself that OutfieldPlayer and Goalkeeper are subtypes of
Player, so I think the above should compile. I haven't seen your real
code so I only have your word for this, and can't test it myself.
Please post the exact message given by the compiler, along with the
line it's complaining about.
 
Z

zcraven

You said yourself that OutfieldPlayer and Goalkeeper are subtypes of
Player, so I think the above should compile. I haven't seen your real
code so I only have your word for this, and can't test it myself.
Please post the exact message given by the compiler, along with the
line it's complaining about.

this is the code:
-----------------------------------------------------------
public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3, OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6, OutfieldPlayer
p7, OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)

{
if (g1 instanceof OutfieldPlayer){
throw new IllegalArgumentException("You cannot put an outfield
player in goal");
}

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper || p4
instanceof Goalkeeper || p5 instanceof Goalkeeper ||
p6 instanceof Goalkeeper || p7 instanceof Goalkeeper || p8
instanceof Goalkeeper || p9 instanceof Goalkeeper ||
p10 instanceof Goalkeeper){
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
}
-----------------------------------------------------

It complains with 'inconvertible types' at this line:

public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3, OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6, OutfieldPlayer
p7, OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)
 
S

Steve Bosman

Without seeing your code, I would guess that you have a class hierarchy
where
Goalkeeper extends Player (and doesn't extend OutfieldPlayer)
and OutfieldPlayer extends Player (and doesn't extend Goalkeeper)

since your method doesn't use the generic Player for the arguments, but
explicitly states Goalkeeper or OutfieldPlayer, the java compiler is
clever enough to know that, to take one example, "p9 instanceof
Goalkeeper" is a nonsense statement since p9 cannot be cast to
Goalkeeper (it is inconvertible).

Steve
 
J

John C. Bollinger

zcraven said:
public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3,
OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6,
OutfieldPlayer p7,
OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)

{
if (g1 instanceof OutfieldPlayer){
throw new IllegalArgumentException("You cannot put an outfield
player in goal");
}

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper || p4
instanceof Goalkeeper || p5 instanceof Goalkeeper ||
p6 instanceof Goalkeeper || p7 instanceof Goalkeeper || p8
instanceof Goalkeeper || p9 instanceof Goalkeeper ||
p10 instanceof Goalkeeper){
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}

This is pointless. You already know that g1 is a Goalkeeper and that p1
- p11 are OutfieldPlayers because those are the declared types of the
method arguments.

Well, it's not *completely* pointless: it will catch null arguments and
throw IllegalArgumentException in that case, but that doesn't appear to
be the intent.

As Joona already wrote, this simply _demands_ an array. If ever you
find yourself writing a method with more than about 3 arguments then you
should consider whether there is a better way. If you ever find
yourself writing a method with more than about 6 arguments then you
should assume that there is a better way and look hard for it. The
better way might be to split the method into two or more smaller
methods, or, as in this case, might be to use a higher-level argument
such as an array or other collective Object. There are other possibilities.

If you wrote:

public void setFirst11(Goalkeeper g, OutfieldPlayer[] ofPlayers) {
[...]

Then you still wouldn't need any argument type checking.


John Bollinger
(e-mail address removed)
 
Z

zcraven

John C. Bollinger said:
zcraven said:
public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3,
OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6,
OutfieldPlayer p7,
OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)

{
if (g1 instanceof OutfieldPlayer){
throw new IllegalArgumentException("You cannot put an outfield
player in goal");
}

if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper || p4
instanceof Goalkeeper || p5 instanceof Goalkeeper ||
p6 instanceof Goalkeeper || p7 instanceof Goalkeeper || p8
instanceof Goalkeeper || p9 instanceof Goalkeeper ||
p10 instanceof Goalkeeper){
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}

This is pointless. You already know that g1 is a Goalkeeper and that p1
- p11 are OutfieldPlayers because those are the declared types of the
method arguments.

Well, it's not *completely* pointless: it will catch null arguments and
throw IllegalArgumentException in that case, but that doesn't appear to
be the intent.

As Joona already wrote, this simply _demands_ an array. If ever you
find yourself writing a method with more than about 3 arguments then you
should consider whether there is a better way. If you ever find
yourself writing a method with more than about 6 arguments then you
should assume that there is a better way and look hard for it. The
better way might be to split the method into two or more smaller
methods, or, as in this case, might be to use a higher-level argument
such as an array or other collective Object. There are other possibilities.

If you wrote:

public void setFirst11(Goalkeeper g, OutfieldPlayer[] ofPlayers) {
[...]

Then you still wouldn't need any argument type checking.

interesting.

If I do this, how do I insert players into that array OutfieldPlayer[]?
 
P

Paul Lutus

zcraven said:
if (p2,p3,p4==Goalkeeper)
{
throw new IllegalArgumentException("You cannot put a
goalkeeper
in an outfield position");
}


I want to check that the variables p2 to p11 are of class OutfieldPlayer
and not Goalkeeper (Both OutfieldPlayer and Goalkeeper are subtypes of
abstract
class Player). How do I write an if to check that the variables are of a
certain class?

Read about instanceof in your documentation. Then post the new, compilable
example code, tell us exactly what you want to do, and ask specific
questions.
 
J

John C. Bollinger

zcraven said:
John C. Bollinger said:
If you wrote:

public void setFirst11(Goalkeeper g, OutfieldPlayer[] ofPlayers) {
[...]

Then you still wouldn't need any argument type checking.


interesting.

If I do this, how do I insert players into that array OutfieldPlayer[]?

Note that I gave you the header of a method _declaration_ not a method
invocation (just to make sure we're on the same wavelength). You need
to create the array before invoking the method, by using the "new"
operator in the normal Java way. There are two variations:

{
[...]
OutfieldPlayer[] parray = new OutfieldPlayer[10];

parray[0] = p0;
parray[1] = p1;
[...]

or

{
[...]
OutfieldPlayer[] parray = new OutfieldPlayer[] { p1, p2, [...] };
[...]

The latter form can be used to create an array at the point of method
invocation as well, without storing a reference in a local variable.
Both of those examples are a bit screwy, however, because the point of
doing it with arrays is to avoid having all the p1 - p11 variables
anywhere in the program, and instead to just use slots in one or more
arrays.


John Bollinger
(e-mail address removed)
 
B

Big Jim

Most important though, you do realise an outfield player can go in goal e.g.
if the keeper gets injured and all the subs have been used.
A goalkeeper can swap with a player and play out too, as long as they notify
the ref of the change.
The Mexican keeper in the last world cup, Campo I think he was called, was
also a very good centre forward!
 
J

Joona I Palaste

zcraven <[email protected]> scribbled the following
this is the code:
-----------------------------------------------------------
public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3, OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6, OutfieldPlayer
p7, OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)
{
if (g1 instanceof OutfieldPlayer){
throw new IllegalArgumentException("You cannot put an outfield
player in goal");
}
if (p2 instanceof Goalkeeper || p3 instanceof Goalkeeper || p4
instanceof Goalkeeper || p5 instanceof Goalkeeper ||
p6 instanceof Goalkeeper || p7 instanceof Goalkeeper || p8
instanceof Goalkeeper || p9 instanceof Goalkeeper ||
p10 instanceof Goalkeeper){
throw new IllegalArgumentException("You cannot put a goalkeeper
in an outfield position");
}
}
-----------------------------------------------------
It complains with 'inconvertible types' at this line:
public void setFirst11(Goalkeeper g1, OutfieldPlayer p2, OutfieldPlayer
p3, OutfieldPlayer p4, OutfieldPlayer p5, OutfieldPlayer p6, OutfieldPlayer
p7, OutfieldPlayer p8, OutfieldPlayer p9, OutfieldPlayer p10,
OutfieldPlayer p11)

Ah yes. If you have defined your method parameters like that, then by
the time your method is even called, you are already guaranteeing to
the compiler that g1 is of type Goalkeeper and p2 through p11 are of
type OutfieldPlayer, so the whole if statement in the method becomes
needless.
I had thought that all parameters, both g1 and p2 through p11, were of
type Player. If they are then the code I showed (for this method) should
compile.
Could you also show the code for the method that is calling this method?
 
Z

zcraven

Joona I Palaste said:
zcraven <[email protected]> scribbled the following







Ah yes. If you have defined your method parameters like that, then by
the time your method is even called, you are already guaranteeing to
the compiler that g1 is of type Goalkeeper and p2 through p11 are of
type OutfieldPlayer, so the whole if statement in the method becomes
needless.
I had thought that all parameters, both g1 and p2 through p11, were of
type Player. If they are then the code I showed (for this method) should
compile.
Could you also show the code for the method that is calling this method?

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I am looking for myself. Have you seen me somewhere?"
- Anon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top