-2

I'm trying to create a phone reader program. If a user were to input a word, the program would output the equivalent phone number. Using while, if, and substrings how would I go about this?

I've tried using while, string replace, and for.

   Scanner keyboard = new Scanner (System.in);

   String phoneWord;
   int length, i ;

   phoneWord = keyboard.nextLine();
   length = phoneWord.length();



   while (i < length) {

I expect the output of the equivalent phone number. ex. if there's an A in the word, it will output 1 in corresponding spot.

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
  • Related: _[What's the difference between JavaScript and Java?](https://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java)_ – Luca Kiebel Feb 13 '19 at 21:28
  • 1
    I did a quick google and there are a number of possible examples you might try the basic idea is, you need to devise away to map between the character and is corresponding numerical value – MadProgrammer Feb 13 '19 at 21:54
  • How do the letters map to number is it `a->1,b->2,...,z->26` or is it mapped like phone pads are `a->1,b->1,...,z->9` ? – ktzr Feb 13 '19 at 22:31

2 Answers2

0

There are several ways to map letters to number. If you want a simple mapping, a:1,b:2,...,i:9,j:10,...,z:26 you can do the following, and take advantage of the fact chars map to ints:

    StringBuilder sb= new StringBuilder();
    for (char letter : phoneWord.toLowerCase().toCharArray()) {
        sb.append((int) letter - 96);
    }
    System.out.println(sb.toString());

You can add a modulus to keep the numbers between 0 and 9 with %10this will map a:1,b:2,...,i:9,j:0,...,z:6

    sb = new StringBuilder();
    for (char letter : phoneWord.toLowerCase().toCharArray()) {
        sb.append(((int) letter - 96) % 10);
    }
    System.out.println(sb.toString());

If you want a custom mapping, such as that of a phone you can create a map like so:

    Map<Character, Integer> letterMap = new HashMap<>();
    letterMap.put('a', 1);
    letterMap.put('b', 1);
    letterMap.put('c', 1);
    letterMap.put('d', 2);
    // ...
    letterMap.put('z', 9);

    sb = new StringBuilder();
    for (char letter : phoneWord.toLowerCase().toCharArray()) {
        sb.append(letterMap.get(letter));
    }
    System.out.println(sb.toString());

I am using .toLowerCase() on the string to reduce the number of letters that need to be mapped. As (int) 'A' = 65 and (int) 'A' = 97

ktzr
  • 1,625
  • 1
  • 11
  • 25
0

This should get you started. Just put the numbers to their corresponding letters and you'll be good to go.

import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static void main(String args[]) throws Exception {

        Scanner keyboard = new Scanner(System.in);

        HashMap<Character, Integer> textToNum = new HashMap<Character, Integer>();

        ///Letters to their corresponding numbers here
        textToNum.put('a', 1);
        textToNum.put('b', 2);
        textToNum.put('c', 3);

        String phoneWord = keyboard.nextLine();
        String result = "";

        for(int i = 0; i < phoneWord.length(); i++) {
            result += textToNum.get(phoneWord.charAt(i));
        }

        System.out.println(result);
    }

}

Hope this helps.

BMasonJ13
  • 108
  • 7