输入少于15位的数字时代码崩溃 [英] The code is crashing when I enter less than 15 digit number

查看:59
本文介绍了输入少于15位的数字时代码崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个应用程序将数字转换为文本,但如果我输入的数字少于15个数字,它就会崩溃。它还会显示它应该是下一个十位。如果我输入的数字大于15位数,则仅需要前15位它不应该做任何事情



I made a app to convert digits to text but it just crashes if I enter less that 15digit number.It also shows the next tens place that it should be.If I enter bigger that 15 digit, It takes first 15 only while it should'nt do anything

package com.example.convertnumbertotext;

import java.math.BigInteger;
import java.text.DecimalFormat;

public class EnglishNumberToWords {

    private static BigInteger bi = new BigInteger("100");
    private static BigInteger bi2 = new BigInteger("20");
    private static BigInteger bi3 = new BigInteger("10");
    private static final String[] tensNames = { "", " ten", " twenty", " thirty", " forty",
            " fifty", " sixty", " seventy", " eighty", " ninety", };

    private static final String[] numNames = { "", " one", " two", " three", " four", " five",
            " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen",
            " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen" };

    private static String convertLessThanOneThousand(BigInteger number) {
        String veryFar = "";
        BigInteger[] bii = number.divideAndRemainder(bi);
        BigInteger[] bii2 = number.divideAndRemainder(bi3);
        int resut = bii[1].compareTo(bi2);

        if (resut == -1) {
            veryFar = numNames[bii[1].intValue()];

            number = number.divide(bi);
        } else if (resut == 1 || resut == 0) {
            veryFar = numNames[bii2[1].intValue()];
            number = number.divide(bi3);

            veryFar = tensNames[bii2[1].intValue()] + veryFar;
            number = number.divide(bi3);
        }
        if (number.compareTo(BigInteger.valueOf(0)) == 0)
            return veryFar;
            return numNames[number.intValue()] + " hundred" + veryFar;
    }

        public static String convert (BigInteger number){
            // 0 to 999 999 999 999
            if (number.compareTo(BigInteger.valueOf(0)) == 0) {
                return "zero";
            }

            String snumber = number.toString();

            // pad with "0"
            String mask = "000000000000";
            DecimalFormat df = new DecimalFormat(mask);
            snumber = df.format(number);

            BigInteger trillions = new BigInteger(snumber.substring(0, 3));
            // nnnXXXnnnnnnnnn
            BigInteger billions = new BigInteger(snumber.substring(3, 6));
            // nnnnnnXXXnnnnnn
            BigInteger millions = new BigInteger(snumber.substring(6, 9));
            // nnnnnnnnnXXXnnn
            BigInteger hundredThousands = new BigInteger(snumber.substring(9, 12));
            // nnnnnnnnnnnnXXX
            BigInteger thousands = new BigInteger(snumber.substring(12, 15));

            String tradTrillions;
            if (trillions.compareTo(BigInteger.valueOf(0)) == 0) {
                tradTrillions = "";
            } else if (trillions.compareTo(BigInteger.valueOf(1)) == 0) {
                tradTrillions = convertLessThanOneThousand(trillions) + " trillion ";
            } else {
                tradTrillions = convertLessThanOneThousand(trillions) + " trillion ";
            }

            String result = tradTrillions;

            String tradBillions;
            if (billions.compareTo(BigInteger.valueOf(0)) == 0) {
                tradBillions = "";
            } else if (billions.compareTo(BigInteger.valueOf(1)) == 0) {
                tradBillions = convertLessThanOneThousand(billions) + " billion ";
            } else {

                tradBillions = convertLessThanOneThousand(billions) + " billion ";
            }
            result = result + tradBillions;

            String tradMillions;
            if (millions.compareTo(BigInteger.valueOf(0)) == 0) {
                tradMillions = "";
            } else if (millions.compareTo(BigInteger.valueOf(1)) == 0) {
                tradMillions = convertLessThanOneThousand(millions) + " million ";
            } else {

                tradMillions = convertLessThanOneThousand(millions) + " million ";
            }
            result = result + tradMillions;

            String tradHundredThousands;
            if (hundredThousands.compareTo(BigInteger.valueOf(0)) == 0) {
                tradHundredThousands = "";
            } else if (hundredThousands.compareTo(BigInteger.valueOf(1)) == 0) {

                tradHundredThousands = "one thousand ";
            } else {
                tradHundredThousands = convertLessThanOneThousand(hundredThousands) + " thousand ";
            }
            result = result + tradHundredThousands;

            String tradThousand;
            tradThousand = convertLessThanOneThousand(thousands);
            result = result + tradThousand;

            // remove extra spaces!
            return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
        }

}




<pre lang="text">





我尝试了什么:



我尝试添加空tenc数组的起始空间。我使用了调试器,它向我展示了数千万,数十亿,数百万和数十万被分配在12位数字时设置断点数千。它应该是下面的一步。我该怎么办修复它



What I have tried:

I tried adding empty space to the starting of the tensname array.I used the debugger, It showed me that trillions, billions, millions and hundred thousands were assigned in 12 digit number when set breakpoint on thousands.It should have been 1 step below.What should I do to fix it

推荐答案

使用调试器检查你在 snumber = df.format(number)中获得的字符数; 。如果它小于15,则代码将在您尝试获得超出结束的子字符串时失败。
Use the debugger to check how many character you get in snumber = df.format(number);. If it is less than 15 then the code will fail at the point when you try to get a substring beyond the end.


引用:

我创建了一个应用程序将数字转换为文本,但如果我输入的数字少于15个数字,它就会崩溃。它还会显示它应该是的下一个十位。如果我输入更大的那个15位数,它只有在它不应该做任何事情时才需要前15个

I made a app to convert digits to text but it just crashes if I enter less that 15digit number.It also shows the next tens place that it should be.If I enter bigger that 15 digit, It takes first 15 only while it should'nt do anything



显示错误信息和位置有助于找出问题所在。



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器上运行你的代码一步一步,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有在调试器中魔术,它不知道你的代码应该做什么,它没有找到bug,它只是帮助哟你要告诉你发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Showing the error message and position can help to find what is the problem.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于输入少于15位的数字时代码崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆