-1

I wrote this simple code:

if (country.contentEquals("France"))
{
    language = "French";
}
else if (country.contentEquals("Germany"))
{
    language = "German";
}
else if (country.contentEquals("USA"))
{
    language = "English";
}

Now I want to change this code using this table:

static String [][] mCountryLanguageTable = {{"France","French"},  
                                            {"German","Germany"},
                                            {"USA","English"}};

Is there any Java type or class that could do this?? This is only a simple exemple that I wrote

ARM
  • 363
  • 1
  • 7
  • 18

5 Answers5

5

I'd recommend using a Map or, better yet, Java Locale. This might be done for you already.

Map<String, String> countryLanguageMap = new HashMap<String, String>() {{
    put("France", "French");
    put("Germany", "German");
    put("USA", "English");
}};

String country = "USA";
String language = countryLanguageMap.get(country);
if (language == null) throw new IllegalArgumentException("No mapping for country " + country);
duffymo
  • 305,152
  • 44
  • 369
  • 561
1

What about this:

String language = null;
String country = ?? //Get your country here

switch(country){
    case "France":
        language = "French";
        break;

    case "US":
        language = "English";
        break;

    case "Germany":
        language = "German";
        break;

    default:
        language = "Not a valid Country selected";
}

System.out.println("Country = " + country + ", Language = " + language);

NOTE: Strings inside switch conditions are supported Java 7 onwards...

Pravat Panda
  • 1,060
  • 2
  • 13
  • 27
  • Note that Android only supports Java 5 or Java 6, not Java 7. Eclipse ADT does not when building for Java 7 but you may have luck with some devices and platform versions with other build tools, see http://stackoverflow.com/questions/7153989/java-7-language-features-with-android for more. – laalto Jun 07 '13 at 13:57
  • @laalto I just expressed a way this can be done in Java as the question says "s there any Java type or class that could do this??" – Pravat Panda Jun 07 '13 at 13:59
  • True. Question tags indicate Android though. – laalto Jun 07 '13 at 14:00
  • uhmmm...missed that :( – Pravat Panda Jun 07 '13 at 14:02
0

What about this one?

public static String getLanguage(String country) {
    for (int i = 0; i < mCountryLanguageTable.length; i++) {
        if (mCountryLanguageTable[i][0].equals(country)) return mCountryLanguageTable[i][1];
    }
    return null;
}
Andremoniy
  • 34,031
  • 20
  • 135
  • 241
  • Well it is the same as the first code I made. I don't want to use if equals return solution. I think The Map solution is interesting – ARM Jun 07 '13 at 13:43
0

I'd recommend using a HashMap. http://docs.oracle.com/javase/6/docs/api/java/util/HashMap.html

HashMaps are associative tables that pair keys to values. In this case, your keys would be countries and values would be languages.

Basic example:

import java.util.*;

public static void main(String[] args) {
    HashMap countryLanguageTable = new HashMap();
    countryLanguageTable.put("France", "French");

    System.out.println(countryLanguageTable.get("France")); //prints "French"
}
Eric Wich
  • 1,504
  • 10
  • 8
0

Best data structure to do such a thing is a Map. If you can change mCountryLanguageTable to a Map, just create a map. Else, here is the code:

    Map<String, String> map = new HashMap<String, String>();
    for (String[] tuple : mCountryLanguageTable) {
        map.put(tuple[0], tuple[1]);
    }
    language = map.get(country);
Toilal
  • 3,301
  • 1
  • 24
  • 32