将数字(String)转换为其整数值

时间:2016-03-16 20:49:26

标签: java arrays converter

编辑:对,我忘了陈述问题 - 这是我将0作为输出的事实。

CONTEXT

我的程序旨在获取用户输入的数字(1-99)并将其输出为整数(即​​34 = 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个字的数字由数十个值组成。一个价值。假设正确的语法[英语],连字符/空格divider之前的单词是十位,divider之后的单词就是那些。

ARRAYS

//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个不同的数组:1个,数十个和青少年。

//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个与上述三个数组相同的其他数组,除了它们存储整数值。

CODE

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

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;

2 个答案:

答案 0 :(得分:2)

在您当前的代码中,您正在char[] tensArray = null;执行类似char[] tensArray = new char[10];的操作,否则最终会使用NPE

它可能效率不高,但这是解决问题的简单方法。

  1. 读取该行并将其拆分为空格(假设您将单词用空格分隔)。

  2. 在上述列表中搜索分割后得到的每个代币,并在答案中添加相应的数字(相同的索引)。

  3. 打印答案。

  4. 以下是代码段:

    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 :(得分:1)

你有一个有趣的方法来解决这个问题。有几件事要改变:

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

  2. 你似乎在使用角色数组做了很多工作,所以我猜你是来自另一种语言。坚持使用字符串可以正常工作。

  3. 你没有解决“青少年”问题。这看起来像是一个简单的疏忽。
  4. 我在尝试维护原始方法时添加了这些修补程序:

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