简体   繁体   中英

if (boolean)unreachable statement in Java

this is for a intro programming class i am taking. I have created a Instance method to add a newValue to the totals. It has two parameters in the method: (a letter identifying the amount type, and amount) I was successful on the first parameter. the second is making me struggle. I am suppose to us an if statement. I made it so there is amount type, then i have three letters that are to be used that can be true. I set the if(amountType == false) and the compiler says its a "unreachable statement". The criteria for the if statement is "If the letter for the amount the is invalid (ie not T, D, or E), throw an IllegalArgumentException, and message back to user.

public double newValue(boolean amountType, double amount)
{
  boolean T = amountType;
  boolean D = amountType;
  boolean E = amountType;


    if (amount < 0) 
    {
  throw new IllegalArgumentException("The amount needs to be 0 or larger");
    }
    return amount;



    if(amountType == false)
        // if not D, E, T.....then exception
    {
        throw new IllegalArgumentException("That is an invalid letter value. "
                + "The data will be ignored");
    } 
    else
    {
    }
}    

Any help would be appreciated.

Your return statement is getting in the way: once executed, any code that falls afterwards will not be executed. It needs to be the last instruction (not literally) to be executed in your method. You can do this instead:

public double newValue(boolean amountType, double amount) {
    boolean T = amountType;
    boolean D = amountType;
    boolean E = amountType;


    if (amount < 0) // Eliminate constraint 1
        throw new IllegalArgumentException("The amount needs to be 0 or larger");

    if (!amountType) // Eliminate constraint 2
        throw new IllegalArgumentException("That is an invalid letter value. "
                + "The data will be ignored");

    // Do your processing, now that you passed all tests

    return amount;
}

You have to put the return amount inside the first if block.

The reason is that if the first if condition is true an exception will be thrown. And if it is evaluated as false , return amount will be executed.

In both cases, the second if block will never be executed

Unreachable means that the line can never be reached in this method. Because you add return statement without if statement, your second if statement can never be execute by program. So move return statement in your first if statement, and it will work.

You have a return amount statement and it always executes and the code after it ie if statement is not reachable because the control always return from return amount. One possible solution is first you have to check for amount type and then in the else part check for amount < 0 statement and the end return it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM