• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing Indices  RSS feed

 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to compare a String array index to a double array's index to get the price of an item.



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?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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". If you are allowed to use the STL, a map would be easier.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wait...if the array "choices" holds items, and the array prices holds the price of the items in the other array, that is a BAD design. it is difficult to implement, and impossible to maintain. things will very easily get out of sync.

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.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could probably do something like this.


Also if you look in the example you posted, you're missing index 3 for your prices array.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Levi Neuxell
Ranch Hand
Posts: 41
Clojure Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Levi Neuxell wrote: . . . Okay, I found a pretty straight-forward way: . . .
Works perfectly, thanks guys!
It is expensive on memory, creating a new List.
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).
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you not come across array initialisers? We can change your code toOf course that is still error‑prone. Because you missed out prices[3], many of those prices are out of synch with the descriptions. You also need to verify that both arrays are the same length.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!