Each value corresponds to the other as item-to-price. What I'm trying to do check if the value of say variable choice is equal to one of the values from the String array, then check that value's index against the double array's indices for a match, afterwards it should return the value of that index. Any idea as to how a guy would go about doing that?
What you should be doing is defining your own class that has both a price and a description.
Also, don't ever store money as a floating point type. You will start getting rounding errors. Money should always be stored as atomic units - in the U.S., that would be in pennies. So store $1.99 as 199, and convert as needed.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Also, don't ever store money as a floating point type. You will start getting rounding errors. Money should always be stored as atomic units - in the U.S., that would be in pennies. So store $1.99 as 199, and convert as needed.
Certainly money shouldn't be stored as a FP type in most real apps, but as an educational exercise I think it's acceptable, as long as the pitfalls are mentioned.
As a proper representation, as an alternative to atomic units, isn't BigDecimal also acceptable?
Keith Lynn wrote:If I understand correctly, you want the user to enter a food like "lemonade", and you then want to determine the price of it. A simple loop could be used to find the index of "lemonade".
This is precisely what I'm trying to do, I just don't know how to make the loop... but I'll surf the net until I find an example to learn off of - thanks! Looping should have been the obvious answer... lol.
Aj Prieto wrote:Also if you look in the example you posted, you're missing index 3 for your prices array.
Thanks for telling me, that probably would've caused some headaches. ^^
Okay, I found a pretty straight-forward way:It seems to work for now, we'll see what happens I guess.
Works perfectly, thanks guys!
-
1
-
-
-
-
It is expensive on memory, creating a new List.Levi Neuxell wrote: . . . Okay, I found a pretty straight-forward way: . . .
Works perfectly, thanks guys!
It is very error‑prone, because a change in the List or the prices array will give incorrect prices for many items.
It does not constitute object‑oriented programming; you were suggested an object‑oriented solution and ignored it.
It is slow; you are using an algorithm which runs in linear time. If you had a sorted array of objects encapsulating description and price, or a Map linking description to item, you could search faster (sorted = logarithmic time, map = constant time).

Why fit in when you were born to stand out? - Seuss. Tiny ad:
Thread Boost - a very different sort of advertising
https://coderanch.com/t/674455/Thread-Boost-feature
|