0

I am a novice programmer and having issues understanding arrays. I have two String arrays:

String[] itemList = {"item1", "item2", "item3",....."item10"}

String[] country = {"US", "UK", "France",....."Germany"}

As you probably understood, the idea is, I should be able to choose an item from itemList, select a countryand the program should show me how much that specific item costs in that country. Now, I can use if statementand say something:

ìf(itemList[i] == "item1" && country[i] == "US"){ //check the price for item1 in the US...}.

This works alright if we have smaller amount of items, but what if the array of items is too big, like hundreds of thousands? The code will be extremely huge if I go with if statement for every item. Can you guys suggest a better solution? Thank you in advance!

xingbin
  • 27,410
  • 9
  • 53
  • 103
sumu00
  • 49
  • 7
  • If you know, that you like to ask for "item1" in "US", why do you need the arrays? What's their usage? Just to test, if country and item are well known? Why do they share the same index 'i'? With 100.000 items in 100 countries, you end with 10.000.000 prices. If the items where sorted by name, you could speed up the search massively. A Map might even be better. – user unknown Mar 04 '18 at 02:00
  • 2
    Also, don't compare `String`s with `==`. – Jacob G. Mar 04 '18 at 02:02
  • @userunknown, hmm??? We don't know which item and country the user chooses. "item1! and "US" is just one option. They can should any item from huge list and any country. – sumu00 Mar 04 '18 at 02:04
  • The compiler should add a default warning to the usage of `==` for `String`s. Probably one of, if not the most common beginner mistake. `==` compares for **identity** and `equals` for *content*, usually you want the latter. – Zabuzard Mar 04 '18 at 02:04
  • @sumu00: Yes, but if you iterate over i, you will only pick item1 for US, item2 for UK and so on, so you need at least 2 indexes. And we don't know, how prices are stored. If you had asked, that the user selected item 435 from the itemlist, country 1 from countrylist, that probably would make more sense, but we still don't know where the price is coming from, which information we need to retrieve it. – user unknown Mar 04 '18 at 02:08

3 Answers3

2

Make an Item object (In a separate class... Item.class)

import java.util.Map;
import java.util.HashMap;

public class Item {
    private final String name;
    // <CountryName, Price>
    private Map<String, Double> countryPrices;

    public Item(String name) {
        this.name = name;
        this.countryPrices = new HashMap<>();
    }

    public String getName() {
        return this.name;
    }
    public void setPrice(String countryName, double price) {
        this.countryPrices.put(countryName, price);
    }
    public double getPrice(String countryName) {
        return this.countryPrices.get(countryName);
    }
}

If this were a serious application, use BigDecimal for more precise prices.

Also, you may want to create a Country enum so you don't have to always make sure you spell them correctly and use correct upper-case / lower-case letters.

To use this, create a list of items:

List<Item> items = new ArrayList<>();

Then to add a new item:

Item someItem = new Item();
someItem.setPrice("USA", 2.99);

Then, to get an item's price depending on a given country name:

// get from user input
String countryName =...
String itemName =...

// loop  through the items list we created
for (Item item : items) {
    if (item.getName().equalsIgnoreCase(itemName)) {
        // we found the item we are looking for! get the price
        double price = item.getPrice(countryName);
        // do what you want with this...
        System.out.println(itemName + "'s price in " + countryName + " is " + price + ".");
        // We are done looking - we found the item and got it's price. End the loop
        break;
    }
}

Sidenote: As @JacobG. said, don't compare strings with ==

MCMastery
  • 3,099
  • 2
  • 20
  • 43
  • Maybe the prices are in a big Map <, Long>, first two as indexes of item, country, last as price in pennies or cents :) – user unknown Mar 04 '18 at 02:10
  • 1
    Thanks all, I will try this. All I wanted to know was if it was a way to get away from nested loop, because it is very inefficient to run nested loop for big arrays – sumu00 Mar 04 '18 at 02:16
  • @userunknown That'd be a good idea, but remember this has to work for different countries – MCMastery Mar 04 '18 at 02:31
  • @MCMastery: I don't think that date format for prices is the topic of the question. It's only worth a side note, if at all. – user unknown Mar 04 '18 at 02:36
0

I got your problem here.

A few things I would like to mention is

  1. String comparisons doesn’t work with ==, double check how and why.
  2. Also you need to work on your logic, if else won’t help if combinations goes beyond something that you can handle. So, I am assuming based on Country and Currency you need to show price of a particular item. I’ll highlight some points for you to think over: Countries are finite while number of Items can change. Also you have to maintain something for Price conversion from Base currency to Countries Currency. Think along those lines and you will be fine.
    1. As you are a newbie to programming, I will suggest not to worry about Length for now. For most cases Arrays would be just fine, and for complex purposes of large lists we use various approaches to handle this, Pagination being most common.

Happy coding!

Yash Soni
  • 456
  • 3
  • 11
0

Assuming, the problem is properly stated:

As you probably understood, the idea is, I should be able to choose an item from itemList, select a country and the program should show me how much that specific item costs in that country. Now, I can use if statementand say something:

If you choose an item from an itemlist, you should already have it's index (let's call it item) in that list.

The same should apply for country (but call the index country).

Now you might have a maybe sparse 2-dim array of prices (called prices), then you could say:

/* datatype of price is left out as a different discussion, might be long
 for penny or cent or float for learning purpose or BigDecimal for bigger prieces. 
*/
price = prices[country][item];

If the prices are stored in a Map, maybe you can query it the same way, maybe by country name, item name. As long as we don't know, we can only guess.

price= prices.get ("US" + ":" + "Java-Compiler"); 

Maybe the prices are in a database, so you would need to query that.

user unknown
  • 35,537
  • 11
  • 75
  • 121