简体   繁体   中英

Need to iterate through two arrays of items for comparison without parallelism

So for my java class I have to write a program that accepts toppings and adds 75 cents to total price for every topping. I haven't bothered for null testing yet because of this error. Essentially I will have:

list1 = Valid Toppings
list2 = User Toppings

User toppings can be entered in any order so it doesn't make any sense to match them as a parallel array. You can see the logic I have used in the code snippet below, but it is not legal. My terminal vomits everywhere when I try to compile. How would I go about implementing the logic below, or perhaps a better way of implementing my logic completely?

  for(x = 0; toppings.length < 3; x++)
    {
      if(toppings[x].equals(for(xx = 0; validToppings[0].length < 3; xx++) {validToppings[xx]})
        {setSize(size); price += .75;}
        else
        {System.out.println("Error in Pizza class: Attempt to set invalid pizza topping" + x + "(" + toppings[x] + ")")};
    }

The problem here is you are trying to use an if statement, which does a single boolean comparison, to a for statement, which can not be made into a boolean.

Do it like this instead:

for(int x = 0; topping.length < 3; x++){
         for(int xx = 0; vaildToppings[0].length < 3; xx++){
             if(toppings[x].equals(validToppings[xx]){
//do stuff
     }
}

Nested for loops are your friend.

topping.length < 3 as your conditions to close is also non nonsensical. Consider reexamining your logic, I don't know what you are actually trying to do, so I'm not going to make any assumptions.

The reason your code is breaking is because you have toppings.length < 3 and validToppings[0].length < 3 when you want, presumably, x < 3 or x < toppings.length

Everything in your for loop should be based on your counter.

Try this:

for (int x = 0; x < toppings.length; x++)
{
    if (!validToppings.contains(toppings[x]))
    {
        //throw some error, or print a message, etc....
    }
    else
    {
        //do stuff
    }
}

A nested for-loop is the right approach here. Don't forget to declare the type of your loop counter in the loop itself (or elsewhere, if you want to use C style), or you'll get compiler errors all day.

for(int x = 0; x < toppings.length; x++) {
    for(int xx = 0; xx < validToppings[0].length; xx++) {
        if(toppings[x].equals(validToppings[xx]) {
            // Perform operations here.
    }
}

As a side note: toppings.length < 3 may or may not be true throughout the loop; it could never be true, thus causing the loop not to execute, or it could be true, causing an infinite loop, regardless of x or xx . Be careful for cases like that.

I'm not sure what you're trying to do. If you are trying to test whether one array of strings is in another array of strings, (as in, a 'subset' in Set Theory). Then why not use ArrayList , something like this: (sorry if my syntax is a little off it's been a while since I did java)

ArrayList<String> toppings = new ArrayList<>();
toppings.add("Pepperoni");
toppings.add("Olives");

ArrayList<String> validToppings = new ArrayList<>();
validToppings.add("Pepperoni");
validToppings.add("Salami");
validToppings.add("Olives");
validToppings.add("Cheese");

boolean toppingsValid = validToppings.containsAll(toppings);

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