vendredi 17 juin 2016

Read input from user and ignore the spaces between words, converting letters to telephone numbers

I've been trying to figure out how to ignore the blank space/digits/letters by using the character.isDigit & character.isLetter method when the users enters a String.. Can you guys please advise me?

When I tried the input with GETLOAN (Without the space) it works well... But when I enter a space between e..g. Get Loan, the program shows an error..

public static void main(String[] args) {

    String letters;
    char phoneDigit;

    Scanner kb = new Scanner(System.in);

    System.out.println("Enter letters : ");
    letters = kb.next();

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

        phoneDigit = letters.charAt(i);

        // Using character.isDigit...
        if (Character.isDigit(phoneDigit) == true || Character.isLetter(phoneDigit) == true);
        {

            if (i == 3) {
                System.out.println("-");
            } //If 

            if (phoneDigit >= 'A' && phoneDigit <= 'C'
                    || phoneDigit >= 'a' && phoneDigit <= 'c') {

                System.out.println("2");

            } else if (phoneDigit >= 'D' && phoneDigit <= 'F'
                    || phoneDigit >= 'd' && phoneDigit <= 'f') {

                System.out.println("3");

            } else if (phoneDigit >= 'G' && phoneDigit <= 'I'
                    || phoneDigit >= 'g' && phoneDigit <= 'i') {

                System.out.println("4");

            } else if (phoneDigit >= 'J' && phoneDigit <= 'L'
                    || phoneDigit >= 'j' && phoneDigit <= 'l') {

                System.out.println("5");

            } else if (phoneDigit >= 'M' && phoneDigit <= 'O'
                    || phoneDigit >= 'm' && phoneDigit <= 'o') {

                System.out.println("6");

            } else if (phoneDigit >= 'P' && phoneDigit <= 'S'
                    || phoneDigit >= 'p' && phoneDigit <= 's') {

                System.out.println("7");

            } else if (phoneDigit >= 'T' && phoneDigit <= 'V'
                    || phoneDigit >= 't' && phoneDigit <= 'v') {

                System.out.println("8");

            } else if (phoneDigit >= 'W' && phoneDigit <= 'Z'
                    || phoneDigit >= 'W' && phoneDigit <= 'z') {

                System.out.println("9");
            } // If
        } // If
    } // For loop

} //PSVM

Aucun commentaire:

Enregistrer un commentaire