Possible solution
In order to solve your problem, use string.startsWith()
instead of string.contains()
:
if (order.startsWith(pizzaSize[0])) {
/* medium */
}
else if (order.startsWith(pizzaSize[1])) {
/* large */
}
else {
/* unexpected input */
}
This works because the first letter always indicates the pizza size. You can stick to using contains()
for the toppings.
pizza
Array
Second, what is going on with your pizza
array? What about elements 0
and 1
?
String[] pizza = new String[7];
pizza[2] = "h";
pizza[3] = "m";
pizza[4] = "o";
pizza[5] = "p";
pizza[6] = "s";
Some other notes
- Use divide and conquer: Split your task up into its logical parts, then tackle each individually. This will make your life easier. If it helps, do it on paper first!
- You don’t need to create a
Scanner
for every iteration of the loop. The same goes for most of the other variables declared at the beginning of the loop. Try to declare those before the loop. - Try to give your variables meaningful names. How is anyone supposed to understand or remember the purpose of
arrayTest
? - You might want to improve your code by using
Enum
,HashMap
or a customTopping
class, unless you don’t feel comfortable using those techniques yet.
solved Java: using .contains but returns two values [closed]