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);
    }
}

You only have one loop, but I think you need two: One loop goes over each character in the input, converting letters to numbers. Another loop is needed to see if the user wants to keep entering more numbers by checking for the '#' character. The first loop should be inside the second, something like this:

loop1 - prompt user to enter a number, check if they entered the '#' char
      loop2 - go over characters in the input, converting letters to numbers
      end loop2
   end loop1

Note that you can use a do...while() loop if you always want the loop to execute at least once (that's a hint for loop1.)

Alternatively, one may use the CANCEL button on the input dialog box:
JOptionPane.showInputDialog(inputMessage); to check if the program should terminate or not.

inputString = JOptionPane.showInputDialog(inputMessage);
        if (inputString== null){
        JOptionPane.showMessageDialog(null, "See you next time",
        "Telephone Digit",JOptionPane.PLAIN_MESSAGE);        	
        	break;
        }

I believe your problem is in the while loop exit condition. I think are only exiting the loop if there is a '#' found; otherwise the loop continues and x is incremented. Try adding a condition to check if x has exceeded the length of the string. Perhaps something like

while (inputString.charAt(x) != '#' && x < inputString.length())

Your check at digit == 7 isn't breaking from the while loop, it is jumping out of the if statement it is nested in. It's been awhile since I worked in Java, but I think I'm remembering this correctly.

Sorry I haven't been able to reply sooner, I have had a ton of work to do of late... anyway. First thank you for all the advice, I have been working on this more and got a little further... Now I can get it to exit out with the '#', and everything works as it should except now after one run instead of the program giving me an error, it just stops and resets. It will sit there doing nothing till I hit run again. Also if I input any numbers with the letters it just sits there doing nothing. Any other advice would be great!
my code now thus far! oh and I was told that previously the error I got was due to the fact that the second time around some variable was not resetting back to zero so I tried to redo that.

import javax.swing.JOptionPane;
public class TelephoneDigitProgram1
{
    public static void main (String[] args)
    {
        char letter;
        String inputMessage = "";
        String inputString = "";
        String outputString = "";
        String outputMessage = "";
        int count;
        int x;
        
        inputMessage = "Enter an input: ";
        inputString =
              JOptionPane.showInputDialog(inputMessage);

    
        while (inputString.charAt(0) != '#')
        {
              count = 0;
              x = 0;
              inputMessage = "";
              outputMessage = "";
             
             
           while (count <= 7)
          {
            letter = Character.toUpperCase(inputString.charAt(x));
           
            
   
            if (letter >= 'A' && letter <= 'Z')
            {
              inputMessage += letter;
                count++;
          x++;
                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";
                        break;
                    default:
                        outputMessage = outputMessage + "Invalid input";
                }
              
                if (count == 7) {
                    break;
                }
                if (count == 3) {
                    outputString += "-";
                }
            }
        }
      
        JOptionPane.showMessageDialog(null, outputString,
                             "Telephone Digit",
                             JOptionPane.PLAIN_MESSAGE);
         
        inputMessage = "Enter another set of letters "
                     + "to find its corresponding "
                     + "telephone number.\n"
                     + "To stop the program enter #.\n"
                     + "Enter a telephone number as letters:";
      
        inputString =
              JOptionPane.showInputDialog(inputMessage);

        letter = inputString.charAt(0);
    
        System.exit(0);
    }
    }
}

not sure why it posted twice! edited so its not the same thing twice!

Your program stops after the first run because on line 109 you have System.exit(0); Get rid of that line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.