如何从这些字符中获取整数值?

时间:2019-07-06 15:50:29

标签: java type-conversion

“我想通过用户输入插入字符。然后我需要获得一个与每个字符相似的整数值?

ex:输入-

thr#e hun#red forty five t#ou#and two hundred th#rty f#ur

输出-

345234

我已经尝试过此代码。”但它会显示一些错误消息。

public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
    String N = input.next();
    int result = Integer.parseInt(N);
    System.out.println(result);

}

}

1 个答案:

答案 0 :(得分:1)

如果这是我的项目,我会尝试将尖锐字符更改为 (#) 为所有英文字母的字符,然后查看哪个最适合该单词,然后将输出更改为字母

示例代码: (将输入更改为字母名称)

public static void main(String[] args) {
    // input o#e tw# thr#e

    char[] alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    String[] numbers = { "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "zero"};
    String input = "o#e tw# thr#e";

    String[] words = input.split(" ");
    for (int i = 0; i < words.length; i++) { // reads one by one the words
        for (int j = 0; j < alphabet.length; j++) { checks if the character[j] fits the word
            for (int k = 0; k < numbers.length; k++) { // checks if the numbers contain that string
                if (numbers[k].equalsIgnoreCase(words[i].replace('#', alphabet[j])))
                    words[i] = numbers[k];
            }
        }
    }
}