简体   繁体   English

为什么这个数字解析器不能正确解析双精度数?

[英]Why does this number parser not correctly parse doubles?

I've written a basic number parser that I believe should work for both doubles and integers. 我写了一个基本的数字解析器,我相信它对双精度和整数都适用。 However, when called to parse a number, it only works with int types, when the number is a double (which it knows by reaching a decimal point) it simply stops parsing. 但是,当调用它解析数字时,它仅适用于int类型,当数字为双精度时(达到小数点即可知道),它只是停止解析。 Can someone please tell me what is wrong with this code: 有人可以告诉我这段代码有什么问题吗:

(Note: the parser is parsing numbers from a file which is previously read into a char array. I know that the contents of the file are read into the array correctly because I print the array contents and it contains the correct contents) (注意:解析器正在解析先前读取到char数组中的文件中的数字。我知道文件的内容已正确读取到数组中,因为我打印了数组内容并且包含正确的内容)

My Number Parser Function: 我的数字解析器功能:

NumReturn numberParser(int cIndex) { // current index of array where num is
    // found
    int num = 0;
    double dnum;
    // int[] result = new int[2];
    while (Character.isDigit(Lexer.fileContents[cIndex]) == true) {
        num = num * 10 + Character.getNumericValue(Lexer.fileContents[cIndex]);
        System.out.println(num);
        cIndex++;
        if (cIndex >= Lexer.fileContents.length)
            break;
    }

    try {
        if (Lexer.fileContents[cIndex] == '.') {
            dnum = (double) num;
            int n = 1;
            while (Character.isDigit(Lexer.fileContents[cIndex++]) == true) {
                dnum = dnum + Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n);
                n++;
                System.out.println(dnum);
                cIndex++;
                if (cIndex >= Lexer.fileContents.length)
                    break;
            }
            System.out.println("Double Value:" + dnum);
            return new NumReturn(dnum, cIndex);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Int Value:" + num);
    return new NumReturn(num, cIndex);

}

NumReturn class: //even though you probably dont need to see this NumReturn类: //即使您可能不需要看到此内容

package jsmash;

public class NumReturn {
    int value;
    double dvalue;
    int pointerLocation;
    NumReturn(int value, int pointerLocation) {
        this.value = value;
        this.pointerLocation = pointerLocation;
    }
    NumReturn(double dvalue, int pointerLocation) {
        this.dvalue = value;
        this.pointerLocation = pointerLocation;
    }
}

Test Cases: 测试用例:

323 --> parses correctly and prints 323
323.54 --> stops parsing after the decimal and prints 323.0 

The problem is this statement: 问题是这样的声明:

dnum = dnum + Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n);

The / operator has higher precedence, so it is evaluated as this: /运算符具有更高的优先级,因此按以下方式进行评估:

dnum = dnum + (Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n));

Both sides of the / operator are of type int , so this is an integer division. /运算符的两侧都是int类型,因此这是一个整数除法。 You are dividing a number < 10 by a number >= 10, so you are always getting 0 as integer division truncates. 您将数字<10除以数字> = 10,因此在整数除法截断时总是得到0。 You need to make at least one of the operands a double , and then it will do floating-point division. 您需要使至少一个操作数成为double ,然后它将进行浮点除法。 However, that's not the end of your problems. 但是,这还不是问题的结局。 Your code divides by 10, 20, 30, .... You want it to divide by 10, 100, 1000, .... Also, you are advancing cIndex twice: inside the while condition, and again in the loop. 您的代码被10、20、30,...除以。您希望它被10、100、1000,...除以。此外,您将cIndex进行了两次:在while条件内,然后在循环中。 You should only do it once. 您只应该执行一次。

        ++cIndex; // Advance past the decimal point.
        double n = 10;
        while (Character.isDigit(Lexer.fileContents[cIndex])) {
            dnum += Character.getNumericValue(Lexer.fileContents[cIndex]) / n;
            n *= 10;

Try changing this line: 尝试更改此行:

dnum = dnum + Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n);

to: 至:

dnum = dnum + (Character.getNumericValue(Lexer.fileContents[cIndex]) * 1.0) / (10 * n);

In your current code, you are dividing Character.getNumericValue(Lexer.fileContents[cIndex]) , which is an int , by a double - this is always equaling 0.0 在当前的代码,你将Character.getNumericValue(Lexer.fileContents[cIndex])这是一个int ,由double -这是始终等于0.0

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

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