Find the error in the following if statement that is intended to select a language for a given country and state/province. (5 points)
language = "English";
if (country.equals("Canada"))
if (stateOrProvince.equals("Quebec"))
language = "French";
else if (country.equals ("China"))
language = "Chinese";
Dag Hammarskjold
05-Oct-2013Braces are important. Your code actually gets executed as
if (country.equals(“Canada”)) {if (stateOrProvince.equals(“Quebec”)) {
language = “French”;
} else if (country.equals (“China”)) {
language = “Chinese”;
}
}
So, your country.equals("China") block would never get executed because within that block country.equals("Canada") is already true. Rewrite your code with proper braces as
if (country.equals(“Canada”)) {if (stateOrProvince.equals(“Quebec”)) {
language = “French”;
}
} else if (country.equals (“China”)) {
language = “Chinese”;
}