简体   繁体   English

使用Character.isLetter和Character.isDigit忽略数字,空格和读取字符串输入

[英]Ignoring digits, blank spaces and read String input, using Character.isLetter & Character.isDigit

I have been stuck for quite some time. 我被困了很长时间。 I would need to ignore digits entered, blank spaces and special characters like $^%##&! 我将需要忽略输入的数字,空格和$ ^%##&之类的特殊字符! and just read the other letters az, using Character.isDigit & Character.isLetter.. I have tried using the both methods, it didn't work out for me.. Please advice.. 并使用Character.isDigit和Character.isLetter阅读其他字母az。我尝试使用这两种方法,但对我来说效果不佳。

The error: 错误:
图1

The normal output (without spaces and digits) : 正常输出(无空格和数字):
图2

The expected output should be 438-5626 even when I entered 123$@GetLoan.. They should ignore the first few characters '123$@' and read only GetLoan.. 即使当我输入123 $ @ GetLoan时,预期输出也应该是438-5626。他们应该忽略前几个字符“ 123 $ @”,而只读GetLoan。

Full Question: Write a program that prompts the user to enter a telephone number expressed in letters and outputs the corresponding telephone number in digits. 完整问题:编写一个程序,提示用户输入字母表示的电话号码,并以数字输出相应的电话号码。 If the user enters more than seven letters, then process only the first seven letters. 如果用户输入的字母超过七个,则仅处理前七个字母。 Also output the – (hyphen) after the third digit. 在第三位数字后也输出–(连字符)。 Allow the user to use both uppercase and lowercase letters as well as spaces between words. 允许用户同时使用大写和小写字母以及单词之间的空格。

public class Question3 {

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

        if (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

Below fragment is probably the problem: 以下片段可能是问题所在:

for (int i = 0; i < 7; i++) {
    phoneDigit = letters.charAt(i);
    if (Character.isLetter(phoneDigit) == true) {

You are taking first 7 characters, and printing only the ones that are letters. 您将采用前7个字符,并仅打印字母。 So for input string 123getloan you will iterate over 123getl , and then isLetter will reject 123 , so your program later will deal only with getl . 因此,对于输入字符串123getloan您将迭代123getl ,然后isLetter将拒绝123 ,因此您的程序以后将仅处理getl

To iterate over only 7 letters you would need to change it to increment i only if given character is a letter eg by doing the below: 要仅迭代7个字母,仅在给定字符是字母的情况下,才需要将其更改为递增i例如通过执行以下操作:

int i = 0;
for (char phoneDigit : letters.toCharArray()) {
    if (Character.isLetter(phoneDigit)) {
        i++;

        // other ifs here

        if (i == 3) {
            System.out.println("-");
        }
    }
    if (i >= 7) {
        break;
    }
}

EDIT: Fixed problem mentioned by @Andreas 编辑:修复了@Andreas提到的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM