-5

I am writing a program that simulates the translation of an alphabetic phone number into just numbers. For example: 888-get-food == 555-438-3663.

Initially, the user should enter the phone number on the following format: 888-GET-FOOD (With the dashes). When I try to check if there are dashes on the user input, it prints the dashes, but with the number 1 in front of it. Very annoying.

This is what I have so far:

// Ask the user to enter the phone number
System.out.println("Please enter the phone number: ");

// Save the phone number into a string
String initialPhoneNumber = input.nextLine();

// Convert user input to UPPERCASE
initialPhoneNumber = initialPhoneNumber.toUpperCase();

// This will be the phone number converted
String finalPhoneNumber = fullPhoneNumber(initialPhoneNumber);

// Print number
System.out.println(initialPhoneNumber);
System.out.println(finalPhoneNumber);

for (int i = 0; i < strLength; i++) {

  char letter = initialPhoneNumber.charAt(i);

  if (Character.isLetter(letter)) {

    switch (letter) {
      case 'A': case 'B': case 'C': number = 2; break;
      case 'D': case 'E': case 'F': number = 3; break;
      case 'G': case 'H': case 'I': number = 4; break;
      case 'J': case 'K': case 'L': number = 5; break;
      case 'M': case 'N': case 'O': number = 6; break;
      case 'P': case 'Q': case 'R': case 'S': number = 7; break;
      case 'T': case 'U': case 'V': number = 8; break;
      case 'W': case 'X': case 'Y': case 'Z': number = 9; break;
      case '-':number='-';

    }
  }

  else if (Character.isDigit(letter)) {
    number = Character.getNumericValue(letter);
  }

  else if (initialPhoneNumber.charAt(i) == '-') {
      number = Character.getNumericValue(letter);
  }

and this is the output:

Please enter the phone number:
555-GET-FOOD
555-GET-FOOD
555-1438-13663

Why does the number 1 show up in front of the dashes? How can I make it so it doesn't show up? In other words, how can I print or separate the numbers separated by dashes?

Thanks

  • 3
    Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Hovercraft Full Of Eels Nov 07 '18 at 15:13
  • 1
    @HovercraftFullOfEels by the same logic, almost every question is a "duplicate" of that. – Michael Nov 07 '18 at 15:16
  • 1
    Have a close look at the code after `else if (initialPhoneNumber.charAt(i) == '-') {` .... – fvu Nov 07 '18 at 15:17
  • 2
    Hint: what value do you expect `Character.getNumericValue('-')` to return? – Jon Skeet Nov 07 '18 at 15:17
  • 3
    @Michael: yes and no. Most questions should show results of debugging attempts prior to asking, if only to isolate the error, and this one doesn't. – Hovercraft Full Of Eels Nov 07 '18 at 15:18
  • 1
    Some feedback to how you should ask questions here (and how you might resolve your questions on your own): Make a [minimal, verifiable example](https://stackoverflow.com/help/mcve), in accordance to SO's rules. Your mistake isn't spread out in the entirety of the code, it's likely only in one specific line. Find and only post this one line, so we don't have to do this work for you. Your problem will most likely resolve itself, then, too. Expending some effort yourself instead of coming here asking us to debug your program is not forbidden. – Nearoo Nov 07 '18 at 15:18
  • 1
    We don't know what `letter` is, we don't know what `i` is, where don't know what `fullPhoneNumber()` does. – Stewart Nov 07 '18 at 15:21
  • 1
    The code is incomplete, is the second half of your code from `fullPhoneNumber`? – Joakim Danielson Nov 07 '18 at 15:22
  • So, what can you recommend to do. –  Nov 07 '18 at 15:37
  • Thanks for your help. –  Nov 07 '18 at 15:58

1 Answers1

3

The problem is within the statement:

else if (initialPhoneNumber.charAt(i) == '-') {
  number = Character.getNumericValue(letter);
}

The method Character.getNumericValue() returrns -1 if the character passed as parameter is not numeric.
Since you're passing -, the method returns -1.
That's why you get the 1 after the dash.

UPDATE

Supposing that the for-loop is part of your method fullPhoneNumber, you could resolve it as follows:

public String fullPhoneNumber(String initialPhoneNumber)
{
  StringBuilder result;
  result = new StringBuilder();

  for (int i = 0; i < initialPhoneNumber.length(); i++)
  {
    char letter = Character.toUpperCase(initialPhoneNumber.charAt(i));
    switch (letter)
    {
      case 'A':
      case 'B':
      case 'C':
        letter = '2';
        break;
      case 'D':
      case 'E':
      case 'F':
        letter = '3';
        break;
      case 'G':
      case 'H':
      case 'I':
        letter = '4';
        break;
      case 'J':
      case 'K':
      case 'L':
        letter = '5';
        break;
      case 'M':
      case 'N':
      case 'O':
        letter = '6';
        break;
      case 'P':
      case 'Q':
      case 'R':
      case 'S':
        letter = '7';
        break;
      case 'T':
      case 'U':
      case 'V':
        letter = '8';
        break;
      case 'W':
      case 'X':
      case 'Y':
      case 'Z':
        letter = '9';
        break;
      case '0':
      case '1':
      case '2':
      case '3':
      case '4':
      case '5':
      case '6':
      case '7':
      case '8':
      case '9':
      case '-':
        break;
      default:
        throw new IllegalArgumentException("");
    }

    result.append(letter);

  } // for

  return (result.toString());

} // fullPhoneNumber
Robert Kock
  • 5,795
  • 1
  • 12
  • 20