Firstly, Hello everyone! Ok so I am going through exercises in my Java book and am having a problem with this one question.
Basically, it wants a user to input a telephone number as letters capital or lower case, spaces or not... eg. Get-Loan which would be 438-5626. So the user inputs 7 letters or more and the program outputs the related phone number with the "-" after the third digit.
So I was writing this and I can get it to run and I can give it 7 letters and it will give me the corresponding phone number. The problems I am having are the program is supposed to continuously run till it is given a "#" and I cannot seem to get it to display my message in the GUI window. Here is what I have done thus far. Sorry if it is a little messy looking, been working on this for a while now and keeping moving/changing things around.
import javax.swing.JOptionPane;
public class TelephoneDigitProgram4
{
public static void main (String[] args)
{
char letter;
String inputMessage = "";
String inputString = "";
String outputString = "";
String outputMessage = "";
int digit = 0;
int x = 0;
inputMessage = "Program to convert "
+ "a string of letters to their corresponding "
+ "telephone digits.\n"
+ "To stop the program enter #.\n"
+ "Enter a phone number as letters:";
inputString =
JOptionPane.showInputDialog(inputString);
while (inputString.charAt(x) != '#')
{
letter = Character.toUpperCase(inputString.charAt(x));
x++;
if (letter >= 'A' && letter <= 'Z')
{
digit++;
switch (letter)
{
case 'A':
case 'B':
case 'C':
outputString += "2";
break;
case 'D':
case 'E':
case 'F':
outputString += "3";
break;
case 'G':
case 'H':
case 'I':
outputString += "4";
break;
case 'J':
case 'K':
case 'L':
outputString += "5";
break;
case 'M':
case 'N':
case 'O':
outputString += "6";
break;
case 'P':
case 'Q':
case 'R':
case 'S':
outputString += "7";
break;
case 'T':
case 'U':
case 'V':
outputString += "8";
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
outputString += "9";
}
if (digit == 7)
{
break;
}
if (digit == 3)
{
outputString += "-";
}
}
}
JOptionPane.showMessageDialog(null, outputString,
"Telephone Digit",
JOptionPane.PLAIN_MESSAGE);
inputMessage = "Enter another uppercase letter "
+ "to find its corresponding "
+ "telephone digit.\n"
+ "To stop the program enter #.\n"
+ "Enter a letter:";
inputString =
JOptionPane.showInputDialog(inputMessage);
letter = inputString.charAt(x);
System.exit(0);
}
}