So I have an assignment where I have to create a Matrix and I have a method that is supposed to return the row in which a char appears. I'm told to throw an IllegalArgumentException if the char is not found in the matrix, but whenever I try to do so, it says Method Call Expected. Why is this?
public int findRow(char c) throws IllegalArgumentException
{
for(int r = 0; r < getKeyTable().length; r++)
{
for(int col = 0; col < getKeyTable()[r].length; col++)
{
if(getKeyTable()[r][col] == c)
return r;
}
}
return throw IllegalArgumentException("uh oh");
}
return throw isn't a thing. Either you return, or you throw.
Additionally, you need new to show that you want to create an instance of the IllegalArgumentException class, rather than invoke some method called IllegalArgumentException (which would be allowed, but inadvisable):
throw new IllegalArgumentException("uh oh");
return either appears by itself (return;, in a void method), or it takes an expression (return something;, in a non-void method). throw something isn't an expression, so it can't appear in a return statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With