Functioning if statement, improving it

if (dog.equalsIgnoreCase("yes")) {
    drink.don.setCost(8.75);
    drink.don.getType();
    drin.l.add(drink.don.getType());
    drink.c.add((double) coke.don.getCost());
    cokeprice = coke + fanta.don.getCost();
else if (dog.equalsIgnoreCase("no"))
else catch(IllegalArgumentException iae) {
    System.out.println("requires yes or no");
}
}

Ignore the stupid naming conventions had to change them, incase any class mates decided to steal anything ;p

I'm trying to get my if statement to allow the user input yes and do a condition, then if "no" has been entered then nothing happens just moves onto the next statement, then anything else is illegal and the program crashes.

Answers


I don't know what language you are using, nor do I know what any of the methods you are using do, but here is an example of a similar statement in C#.

First, use a method to convert the user input to a true or false (boolean) value:

public static bool IsYes (string userInput)
{
    if (userInput == "yes')
    {
         return true;
    }
    else  if (userInput == "no")
    {
        return false;
    }
    else
    {
        throw new  CustomException();
    }
}

Next, you can take the result of IsYes() and use it for the if else statement:

if (IsYes(userInput))
{
    // code you want to execute if "yes" 
}
else
{
    // code you want to execute if "no"
}

Hopefully this code will give you an idea of how to use if-else statements, but in the future please explain your question more clearely. Remember, this is C#, so although if statements are similar in almost all languages some of the other code will differ. Also, this is just an example, it won't do anything on its own.


I don't like throwing exceptions, especially if I expect that the user might type in something that I don't want. I'd rather do something like

if (userInput.equalsIgnoreCase("Yes")) {
    // do yes
}
else if (userInput.equalsIgnoreCase("No")) {
    // do no
}
else {
    // Sorry, invalid input
}

Need Your Help

Python MultiThreading With Urllib2 Issue

python multithreading sockets urllib

I can download multiple files quite fast with many threads at once but the problem is that after a few minutes it tends to slow down gradually to almost a full stop, I have no idea why. There's not...

Convert String (representing decimal number) to long

java string parsing converter long-integer

I've googled around a bit but could not find examples to find a solution. Here is my problem: