3

So I had to write a program that mimics a phone keypad, whereas it would convert a string of text to integers: abc(2), def(3), ghi(4), jkl(5), mno(6), pqrs(7), tuv(8), wxyz(9). Except the output has to have hyphens(-) between the digits.

Example input: Alabama

Output: 2-5-2-2-2-6-2

But I just only output 2522262. How would I go about formatting this correctly?

public class Keypad {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    System.out.print("Enter a string: ");
    String s = sc.nextLine();

    System.out.println(getNumbers(s));
}

public static String getNumbers(String s) {
    String result = new String();

    for (int i = 0; i < s.length(); i++) {
        if (Character.isLetter(s.charAt(i))) {
        result += getNumber(Character.toUpperCase(s.charAt(i)));
        }
        else {
            result += s.charAt(i);
        }
    }
    return result;
}

public static int getNumber(char upperCaseLetter) {
    int number = ((upperCaseLetter - 'A') / 3) + 2;
    if (number < 7) {
        return number;
    }
    else if (upperCaseLetter - 'A' < 20) {
        return 7;
    }
    else if (upperCaseLetter - 'A' < 23) {
        return 8;
    }
    else {
        return 9;
    }
  }
}
Lukas
  • 65
  • 3
  • 9
  • 1
    If you use Java 8, this question have already an answer [String.join](http://stackoverflow.com/questions/1978933/a-quick-and-easy-way-to-join-array-elements-with-a-separator-the-opposite-of-sp) –  Mar 06 '17 at 20:32
  • @ÉricRoberge this is a good, can do it in one line without have to remove the last char etc. – Eddie Martinez Mar 06 '17 at 21:06

4 Answers4

2

Go to the place you construct the result and add the hyphen:

    result += getNumber(Character.toUpperCase(s.charAt(i)));
    result += "-";

Then before you return you will have to strip off the last hyphen:

    return result.substring(0, result.length() - 1);

So the whole method would look like this:

public static String getNumbers(String s) {
    String result = new String();

    for (int i = 0; i < s.length(); i++) {
        if (Character.isLetter(s.charAt(i))) {
            result += getNumber(Character.toUpperCase(s.charAt(i)));
            result += "-";
        }
        else {
            result += s.charAt(i);
        }
    }
    return result.substring(0, result.length() - 1);
}
nhouser9
  • 6,730
  • 3
  • 21
  • 42
1

Change the code in getNumbers(String) to

 for (int i = 0; i < s.length(); i++) {
    if (Character.isLetter(s.charAt(i))) {
    result += getNumber(Character.toUpperCase(s.charAt(i)));
    if (i < (s.length-1)
       result += '-";
    }
}
return result;

}

Dakoda
  • 175
  • 7
1

There is a method in Java 8 that does just this. Use String.join, docs, to add a dash after each character.

public static String getNumbers(String s) {
        String result = new String();

        for (int i = 0; i < s.length(); i++) {
            if (Character.isLetter(s.charAt(i))) {
            result += getNumber(Character.toUpperCase(s.charAt(i)));
            }
            else {
                result += s.charAt(i);
            }
        }

        return String.join("-", result.split("");
    }

Note

You should try to avoid using the += with strings, StringBuffer provides better performance. When you concatenate strings you are actually creating new objects for each new string you are concatenating. Imagine a large loop you will have n objects to create the new string.

Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
1

StringJoiner was added to Java 8 for this purpose. Very simple and straightforward to use:

StringJoiner sj = new StringJoiner("-", "", "");
sj.add("1").add("1").add("2");
String desiredString = sj.toString();

or with Stream API, which might be a little more convenient in your case:

List<Integer> integers = Arrays.asList(1,2,3,4,5);
String hyphenSeparatedNumbers = integers.stream()
    .map(Object::toString)
    .collect(Collectors.joining("-"));

Also String.join is a superb alternative for this task.

Anton Arhipov
  • 6,479
  • 1
  • 35
  • 43