223k views
5 votes
Output "Fruit" if the value of userItem is a type of fruit. Otherwise, if the value of userItem is a type of drink, output "Drink". End each output with a newline.

Ex: If userItem is GR_APPLES, then the output is:
Fruit Java Please

1 Answer

4 votes
To solve the problem, we need to check if the value of userItem is a type of fruit or drink. We can do this using an if-else statement and the endsWith method of the String class to check the suffix of the value.

Here's the pseudocode to solve the problem:

If userItem ends with "APPLES", "BANANAS", or "ORANGES", output "Fruit" and a newline.
Else if userItem ends with "COFFEE", "TEA", or "SODA", output "Drink" and a newline.
Otherwise, output nothing.
Here's the corresponding Java code:

if (userItem.endsWith("APPLES") || userItem.endsWith("BANANAS") || userItem.endsWith("ORANGES")) {
System.out.println("Fruit");
} else if (userItem.endsWith("COFFEE") || userItem.endsWith("TEA") || userItem.endsWith("SODA")) {
System.out.println("Drink");
} else {
// do nothing
}

Note that we're using the || (or) operator to check if the value ends with any of the specified fruits or drinks. If it does, we output the corresponding type. If not, we do nothing.
User Mpeac
by
8.9k points