Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program to convert an alphabetic telephone number into strictly n

ID: 3631966 • Letter: I

Question

I am writing a program to convert an alphabetic telephone number into strictly numerical. How do I get it to display in a single dialog box rather than each digit seperately? Here is my code so far. Thanks in advance!

import javax.swing.*;

public class Assignment101
{

public static void main(String[] args)
{


int i;
String number = JOptionPane.showInputDialog(null, "Enter phone number: ");

i=0;
while(i<number.length())
{switch(number.charAt(i))
{case 'A':case 'B':case 'C':
JOptionPane.showMessageDialog(null,"2");
break;
case 'D':case 'E':case 'F':
JOptionPane.showMessageDialog(null,"3");
break;
case 'G':case 'H':case 'I':
JOptionPane.showMessageDialog(null,"4");
break;
case 'J':case 'K':case 'L':
JOptionPane.showMessageDialog(null,"5");
break;
case 'M':case 'N':case 'O':
JOptionPane.showMessageDialog(null,"6");
break;
case 'P':case 'R':case 'S':
JOptionPane.showMessageDialog(null,"7");
break;
case 'T':case 'U':case 'V':
JOptionPane.showMessageDialog(null,"8");
break;
case 'W':case 'X':case 'Y':
JOptionPane.showMessageDialog(null,"9");
break;
default:
JOptionPane.showMessageDialog(null, number.charAt(i));
}
i++;
}
}
}

Explanation / Answer

So what you want is to take the number as alphabet, convert each character to its digit number equivalent, and display the whole thing after conversion? I'll take that as a yes :)
In that case, you can use the switch case statement to replace each character ai index i with its digit equivament - also as a character. Then once done with the entire conversion, display the full converted string in the MessageDialog.
If for some reason you need to convert that digitized string into an actual numeric object, you can use :
int i = Integer.parseInt("123");
long l = Long.parseLong("123456789");
I hope this is what you needed - if so then please remember to rate :) - otherwise send me a PM to inbox and we can look into it

import javax.swing.*;

public class Assignment101
{

public static void main(String[] args)
{
int i;
        String number = JOptionPane.showInputDialog(null, "Enter phone number: ");
        String converted = "";
       
        i = 0;
        while (i < number.length())
        {
            switch(number.charAt(i))
            {
                case 'A':
                case 'B':
                case 'C':
                    converted+="2";; break;
                case 'D':
                case 'E':
                case 'F':
                    converted+= "3"; break;
                    case 'G':
                case 'H':
                case 'I':
                    converted += "4"; break;
                    case 'J':
                case 'K':
                case 'L':
                    converted += "5"; break;
                    case 'M':
                case 'N':
                case 'O':
                    converted += "6"; break;
                case 'P':
                case 'R':
                case 'S':
                    converted += "7"; break;
                case 'T':
                case 'U':
                case 'V':
                    converted += "8"; break;
                case 'W':
                case 'X':
                case 'Y':
                    converted += "8";
                    break;
                default:
                    converted += number.charAt(i);break;
            }
            i++;
        }
       
        System.out.println(converted);
        JOptionPane.showMessageDialog(null, converted);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote