Java 为什么这个数字分析器不能正确地解析double?

Java 为什么这个数字分析器不能正确地解析double?,java,parsing,Java,Parsing,我已经写了一个基本的数字解析器,我相信它应该同时适用于双精度和整数。然而,当调用它来解析一个数字时,它只适用于int类型,当这个数字是一个double(它通过到达小数点来知道)时,它只是停止解析。有人能告诉我这个代码有什么问题吗 (注意:解析器正在解析以前读入char数组的文件中的数字。我知道该文件的内容被正确读入数组,因为我打印了数组内容,并且它包含正确的内容) 我的数字解析器函数: NumReturn numberParser(int cIndex) { // current index o

我已经写了一个基本的数字解析器,我相信它应该同时适用于双精度和整数。然而,当调用它来解析一个数字时,它只适用于int类型,当这个数字是一个double(它通过到达小数点来知道)时,它只是停止解析。有人能告诉我这个代码有什么问题吗

(注意:解析器正在解析以前读入char数组的文件中的数字。我知道该文件的内容被正确读入数组,因为我打印了数组内容,并且它包含正确的内容)

我的数字解析器函数:

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

}
323 --> parses correctly and prints 323
323.54 --> stops parsing after the decimal and prints 323.0 
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;
    }
}
测试用例:

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

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

尝试更改此行:

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


在当前代码中,您正在将
字符.getNumericValue(Lexer.fileContents[cIndex])
除以
双精度
-这始终等于
0.0

尝试更改此行:

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


在当前代码中,您将
字符.getNumericValue(Lexer.fileContents[cIndex])
除以
双精度
-这始终等于
0.0

问题在于以下语句:

dnum = dnum + Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n);
/
运算符具有更高的优先级,因此其计算如下:

dnum = dnum + (Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n));
/
运算符的两侧都是
int
类型,因此这是一个整数除法。您将一个<10的数字除以一个>=10的数字,因此当整数除法截断时,您总是得到0。您需要将至少一个操作数设为双精度,然后它将执行浮点除法。然而,这并不是你问题的结束。你的代码除以10,20,30。。。。你想让它除以10,100,1000。。。。此外,您将推进cIndex两次:在while条件内,再次在循环中。你应该只做一次

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

问题在于这种说法:

dnum = dnum + Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n);
/
运算符具有更高的优先级,因此其计算如下:

dnum = dnum + (Character.getNumericValue(Lexer.fileContents[cIndex]) / (10 * n));
/
运算符的两侧都是
int
类型,因此这是一个整数除法。您将一个<10的数字除以一个>=10的数字,因此当整数除法截断时,您总是得到0。您需要将至少一个操作数设为双精度,然后它将执行浮点除法。然而,这并不是你问题的结束。你的代码除以10,20,30。。。。你想让它除以10,100,1000。。。。此外,您将推进cIndex两次:在while条件内,再次在循环中。你应该只做一次

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

啊,我怎么会是这样一个白痴。彭达斯哈哈,谢谢你。因为我不在电脑上,所以还没有机会尝试,所以我还没有接受这个答案。我一有机会测试就做。我在最后几分钟对我的答案做了一些更正。我还没有亲自测试过,但我想现在应该可以了。好的,再次感谢。帮助很大:D我已经被困在这个问题上好几天了。啊,我怎么会是这样一个白痴。彭达斯哈哈,谢谢你。因为我不在电脑上,所以还没有机会尝试,所以我还没有接受这个答案。我一有机会测试就做。我在最后几分钟对我的答案做了一些更正。我还没有亲自测试过,但我想现在应该可以了。好的,再次感谢。帮助很大:D我已经被困在这个问题上好几天了。谢谢你的快速回复。我真的很感谢你的帮助。不过,我会接受另一个答案,因为它有更多的细节,未来任何人都可能从中得到更多。感谢您的快速回复。我真的很感谢你的帮助。然而,我会接受另一个答案,因为它有更多的细节,未来任何有这个问题的人都可能从中得到更多。