user2932587
user2932587

Reputation:

Storing multiple phone numbers in a single array in Java

I'm working on a program and I'm stuck. This particular program is supposed to prompt the user to input either an uppercase or lowercase Y to process a phone number, they are then prompted to enter the character representation of a phone number (like CALL HOME would be 225-5466), this repeats until the user enters something other than the letter Y. All of the words entered are to be stored into a single array. The program is then to convert these words in the array to actual phone numbers. I'm kind of confused as to how to set this up. Here is what I have so far.

        import java.util.*;
    public class Program1 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
            String begin;
            int phoneNumber = number.convertNum(); 
            System.out.println("Please enter an uppercase or lowercase Y when you are ready to enter a telephone number: ");
                begin = input.next();
                while (begin.equals("y") || begin.equals("Y")) {
            System.out.println("Please enter a telephone number expressed in letters: ");
              String letters = input.next();
              char[] phoneArray = letters.toCharArray();
            System.out.println("Please enter an uppercase or lowercase Y when you are ready to enter a telephone number: ");
            begin = input.next();
                }
            System.out.println("Here is the numeric representation of the phone number(s) you entered");
    }

    public static int convertNum(char[] phoneArray) { 


        return number;
    }
} 

I realize that the second method doesn't have anything to return yet, I just wanted to put that in there while I was doing things that I actually know how to do ha. The convertNum() method is supposed to be the method with which the array of characters is converted to phone numbers.

I'm still trying to think my way through this. I would think it'd be easier to store the inputs from the user as individual letters rather than words for the sake of converting to phone numbers.

Also, we are only supposed to recognize the first 7 letters of each word that is entered, like the CALL HOME example, there are 8 letters but it's still a seven digit phone number, so I'm not sure how that would work.

Also, when printing the phone numbers after they've been converted, we're supposed to print the hyphen after the third number, I have no clue how that would be done from an array.

I was actually feeling pretty good about this program until I reached this point ha. Any help would be greatly appreciated.

Upvotes: 0

Views: 2790

Answers (2)

aliteralmind
aliteralmind

Reputation: 20163

To translate a character to it's proper number, you can use a map, where the key is a character, and the value is the digit (in string form) that it represents.

Map<Character,String> charToPhoneDigitMap = new HashMap<Character,String>();
charToPhoneDigitMap.put('A', "2");
charToPhoneDigitMap.put('B', "2");
charToPhoneDigitMap.put('C', "2");
charToPhoneDigitMap.put('D', "3");
...
charToPhoneDigitMap.put('W', "9");
charToPhoneDigitMap.put('X', "9");
charToPhoneDigitMap.put('Y', "9");
charToPhoneDigitMap.put('Z', null);

So, for each character in the input string, ignoring any spaces and dashes, get it's digit:

String digitStr = charToPhoneDigitMap.get(inputChar);
if(digitStr == null)  {
     throw  new IllegalArgumentException("'" + inputChar + "' has no corresponding phone number digit.");
 }
 phoneNumString += digitStr;    

When the length of phoneNumString reaches 7, stop.

As far as displaying the dash: Don't store the dash, just store the raw digits.

Create a getPhoneNumberWithDash(raw_phoneNumStr) function, that returns it with the dash in the right place.

Now, if you also want to store the letter-representation, then you might want an array of a PhoneNumbers, instead of just a string-array containing only the raw phone numbers (seven digits each):

public class PhoneNumber  {
   private final String digits;
   private final String letters;
   public PhoneNumber(String digits, String letters)  {
      this.digits = digits;
      this.letters = letters;
   }
   public String getLetters()  {
      return  letters;
   }
   String getDigits()  {
      return  digits;
   }
}

Upvotes: 2

vlatkozelka
vlatkozelka

Reputation: 999

A better approach to this would be to make a class called PhoneNumber for example :

public class PhoneNumber {
    String name;  //the representation like "Call Home"
    String number;//the actual phone number like "225-5466"

  //constructor ...
  }

and then store create instances of PhoneNumber and store them in a List or ArrayList

public static void main(String args[]){

    ArrayList<PhoneNumber> numbersList = new ArrayList();
    String name;
    String number;
    Scanner input = new Scanner(System.in);
    System.out.println("input number representation");
    name=input.nextLine();
    System.out.println("input number ");
    number=input.nextLine();
    numbersList.add(new PhoneNumber(name,number));
    System.out.println(numbersList.get(0).name+" represents :"+numbersList.get(0).number);

    }

Upvotes: 1

Related Questions