0
private void convertToNumeric() {

    for (int i = 0; i < this.CONVERT_TO_NUMERIC.length; i++) {
        for (int j = 0; j < this.INPUT_FROM_USER.length(); j++) {



        }
    }



}



private String INPUT_FROM_USER = "";
private final String[][] CONVERT_VALUES = { {"1", "w"}, { "2", "e"}, {"3", "r"}, 
                                            {"4", "s"}, {"5", "d"}, {"6", "f"}, {"7", "z"},
                                            { "8", "x"}, {"9", "c"} };

private final String[][] CONVERT_TO_NUMERIC = { {"abc", "2"}, {"def", "3"}, {"ghi", "4"},
                                                {"jkl", "5"}, {"mno", "6"}, {"pqrs", "7"},
                                                {"tuv", "8"}, {"wxyz", "9"} };

Can someone please give me some guidance on how I'd go about converting an input to the character specified in the String arrays?

Here's an example.

  • User inputs "Hello world"
  • Searches the String array and converts each character in the String to the given number
  • Outputs "43556 96753"

If the first value of the array contains the character, it will get replaced with the number.

I'm not asking to be spoonfed, I just need a little guidance on how to get started. Thanks in advance.

user1848712
  • 105
  • 2
  • 9
  • Is that character to number 1 to 1 mapping , I ask this because I see two arrays `CONVERT_TO_NUMERIC ` and `CONVERT_VALUES ` ! – AllTooSir May 27 '13 at 11:36
  • How "Hello World" becomes "43556 96753" . Is 'h'='4', 'e'='3' ? – AllTooSir May 27 '13 at 11:37
  • Each character is replaced with the corresponding number in CONVERT_TO_NUMERIC, if the first String contains the letter, it's replace with the number. – user1848712 May 27 '13 at 12:10

2 Answers2

1

This seems like a job, far better suited to a HashMap. Here is the documentation.

For example, you can use:

HashMap<String, String> map = new HashMap<String, String>();

map.put("H", "10");
map.put("I", "11");

Then when an input string arrives:

String value = "Hello";
String response = "";

for(char c : value)
{
    response += map.get(String.valueOf(c));
}
christopher
  • 26,815
  • 5
  • 55
  • 89
  • Is there a way to use the contains method with HashMap or something similar? Because I'm using values.put("abc", "2"); and it's returning null when I'm trying to get the values because it doesn't exist – user1848712 May 27 '13 at 12:11
  • [This question](http://stackoverflow.com/questions/3430170/how-to-create-a-2-way-map-in-java) seems to point you in the right direction. – christopher May 27 '13 at 13:25
1

Turn it around, and start with the tables (= what to do):

String value = "Hello world";

for (String[] mapping : CONVERT_TO_NUMERIC) {
    value = value.replaceAll("(?i)[" + mapping[0] + "]", mapping[1]);
    // Case insensitive "[abc]" -> "2" etcera.
}

// value now is "43556 96753"

The same for the other table now.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Yeah thanks, except I'm not trying to replace the whole String, but each individual character. I'm not sure if this will work in the way I want it to – user1848712 May 27 '13 at 12:12