将数字字(字符串)转换为其整数值

Convert a number-word (String) to its Integer Value

编辑: 对,我忘了说明问题——这是我得到 0 作为输出的事实。

上下文

我的程序旨在获取用户输入的数字字 (1- 99) 并将其输出为整数(即​​三十四 = 34)。我无法弄清楚我的代码中的错误在哪里,需要帮助:

Scanner scInput = new Scanner(System.in);
String word = scInput.nextLine(); //number in word-form (i.e. twenty six)

char[] charArray = word.toCharArray();//string to char array for word^
int divider = 0; //position of hyphen/space in charArray

所有 2 字数字均由一个十位值和一个个位值组成。假设语法正确 [english],hyphen/space divider 之前的词是十位,divider 之后的词是个位。

数组

//word values - components & syntax (1-99)

//ONES
public static final String[] wONES = {"one","two","three","four","five","six","seven","eight","nine"};

//TENS
public static final String[] wTENS = {null,"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; 

//TEENS
public static final String[] wTEENS = {"ten", "eleven", "twelve", "thirteen","fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}; 

我已将所有单词成分组织成 3 个不同的数组:个位、十位和十位。

//equivalent integer-array  of above String arrays

//ONES
public static final int[] nONES = {1,2,3,4,5,6,7,8,9};

//TENS
public static final int[] nTENS = {0,20,30,40,50,60,70,80,90};

//TEENS
public static final int[] nTEENS = {10,11,12,13,14,15,16,17,18,19};

我创建了另外 3 个与上述三个数组相同的数组,除了它们存储整数值。

代码

这里我将用户输入的字符串分成两部分:十位和个位。因此,如果数字是 72:70 = 十和 2 = 个。

int tensValue = 0; //number's tens value (i.e. 30)
int onesValue = 0; //ones value (i.e. 3)

        char[] tensArray = null; //array storing tens section of word (before divider)
        for (int u = 0; u < divider; u++){
            tensArray[u] = charArray[u];
        }
        String tens = new String(tensArray); //convert char array to String


        char[] onesArray = null; //array storing ones section of word (after divider)
        for (int u = divider + 1; u > divider && u < charArray.length; u++){
            onesArray[u] = charArray[u];
        }
        String ones = new String(onesArray);

        //searches for matches in String array for tens
        for(int u = 0; u < wTENS.length; u++){
            if(tens.equals(wTENS[u])){
                tensValue = nTENS[u];
                total += tensValue;

            }
        }

        //searches for matches in String array for ones
        for(int u = 0; u < wONES.length; u++){
            if(ones.equals(wONES[u])){
                onesValue = nONES[u];
                total += onesValue;

在您当前的代码中,您正在执行 char[] tensArray = null;,这应该类似于 char[] tensArray = new char[10];,否则您最终会得到 NPE

它可能不是最有效的,但这是解决您的问题的一种简单且更好的方法。

  1. 阅读该行并将其拆分为白色 space(假设您用 space 分隔您的单词)。

  2. 在上面的列表中搜索拆分后得到的每个标记,并将相应的数字(相同索引)添加到您的答案中。

  3. 打印答案。

这是代码片段:

class Main
{
    public static final String[] wONES = {"one","two","three","four","five","six",
                                          "seven","eight","nine"};
    public static final String[] wTENS = {"ten","twenty","thirty","forty","fifty","sixty",
                                          "seventy","eighty","ninety"}; 
    public static final String[] wTEENS = {"eleven", "twelve", "thirteen","fourteen",
                                           "fifteen", "sixteen", "seventeen", "eighteen", 
                                           "nineteen"}; 

    public static final int[] nONES = {1,2,3,4,5,6,7,8,9};
    public static final int[] nTENS = {10,20,30,40,50,60,70,80,90};
    public static final int[] nTEENS = {11,12,13,14,15,16,17,18,19};

    public static void main (String[] args) throws Exception
    {
        Scanner scInput = new Scanner(System.in);
        String word = scInput.nextLine();
        int answer = 0;

        /* Assuming you are giving space between words */
        for(String s : word.split(" ")) {
            /* Scan wONES */
            for(int i = 0; i < wONES.length; i++) {
                if(wONES[i].equalsIgnoreCase(s)) {
                    answer += nONES[i];
                    continue;
                }
            }

            /* Scan wTENS */
            for(int i = 0; i < wTENS.length; i++) {
                if(wTENS[i].equalsIgnoreCase(s)) {
                    answer += nTENS[i];
                    continue;
                }
            }

            /* Scan wTEENS */
            for(int i = 0; i < wTEENS.length; i++) {
                if(wTEENS[i].equalsIgnoreCase(s)) {
                    answer += nTEENS[i];
                    continue;
                }
            }
        }

        System.out.println("Result: " + answer);
    }
}

输入:

thirty four

输出:

34

你对这个问题有一个有趣的方法。要更改的几件事:

  1. 我没看到你设置分隔索引的位置。

  2. 您似乎对字符数组做了很多工作,所以我猜您是从另一种语言过来的。坚持使用字符串会很好。

  3. 您没有解决 "teens"。这看起来像是一个简单的疏忽。

我在尝试维护原始方法的同时添加了这些修复程序:

public static void main(String [] args) {

    Scanner scInput = new Scanner(System.in);
    String word = scInput.nextLine();

    int total = 0;
    int tensValue = 0; //number's tens value (i.e. 30)
    int onesValue = 0; //ones value (i.e. 3)

    int divider = word.indexOf('-');
    String tens = null;
    String ones = null;
    if (divider != -1) {
        tens = word.substring(0, divider);
        ones = word.substring(divider + 1);
    } else {
        ones = word;
    }

    //searches for matches in String array for tens
    if (tens != null) {
        for (int u = 0; u < wTENS.length; u++) {
            if (tens.equals(wTENS[u])) {
                tensValue = nTENS[u];
                total += tensValue;
            }
        }
    }

    //searches for matches in String array for ones
    for(int u = 0; u < wONES.length; u++) {
        if (ones.equals(wONES[u])) {
            onesValue = nONES[u];
            total += onesValue;
        }
    }

    // if a "teen" override what's in total
    for(int u = 0; u < wTEENS.length; u++) {
     if (ones.equals(wTEENS[u])) {
         total = nTEENS[u];
     }
    }

    System.out.println(total);
}