#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Scanner to JOptionPane


    I'm having difficulty converting a program from scanner to JOptionPane. Please help.

    (Phone keypads) The program translates letters and numbers to digits and leaves all other characters intact.

    Here is a sample run of the program
    ---------------------------
    Enter a string: 1-800-Flowers: [Enter]
    1-800-3569377
    ----------------------------

    My program is not printing the output on JOptionPane screen.

    Here is my code so far.


    import javax.swing.JOptionPane;
    import java.util.Scanner;

    public class Exercise9_7Bulger {
    public static int getNumber(char uppercaseLetter) {
    int number = 0;
    switch (uppercaseLetter) {
    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;
    }

    return number;
    }

    public static void main(String[] args) {

    String input1 = JOptionPane.showInputDialog ("Enter a string: ");

    JOptionPane.showMessageDialog (null, input1);

    // this was on Scanner //System.out.print("Enter a string: ");

    // this was on Scanner //Scanner scanner = new Scanner(System.in);

    // JOptionPane scanner = new JOptionPane(System.in);

    // this was on Scanner // String string = scanner.nextLine();
    // this was on Scanner // scanner.close();

    int stringLength = input1.length();
    for (int index = 0; index < stringLength; index++) {
    char currChar = input1.charAt(index);

    if (Character.isLetter(currChar)) {
    // Make sure the character is uppercase.
    currChar = Character.toUpperCase(currChar);

    // Conver the dialed letter into a number.
    int number = getNumber(currChar);

    // Print the number
    JOptionPane.showMessageDialog(null, number);
    // this was on Scanner //System.out.print(number);
    } else {
    // The character was not a letter, so print it unmodified.

    JOptionPane.showMessageDialog(null, currChar);

    // this was on Scanner // System.out.print(currChar);
    }

    }
    JOptionPane.showMessageDialog(null, "");

    // this was on Scanner //System.out.println();
    }

    }
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    73
    Rep Power
    16
    It compiles and runs for me
    I see each number displayed one after the other in a message box.

    If you want to build up the converted result to display in one go, you can use a StringBuilder to construct it:

    Code:
        public static void main(String[] args) {
    
            String input1 = JOptionPane.showInputDialog("Enter a string: ");
    
            JOptionPane.showMessageDialog(null, input1);
    
            int stringLength = input1.length();
            StringBuilder sb = new StringBuilder();
            for (int index = 0; index < stringLength; index++) {
                char currChar = input1.charAt(index);
    
                if (Character.isLetter(currChar)) {
                    currChar = Character.toUpperCase(currChar);
                    int number = getNumber(currChar);
                    sb.append(number);
                } else {
                    sb.append(currChar);
                }
    
            }
            JOptionPane.showMessageDialog(null, sb.toString());
        }
    Hope this helps.
    slink
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    StringBuilder was what I needed


    That is exactly what I wanted it to do. Thanks. You made my day!

IMN logo majestic logo threadwatch logo seochat tools logo