重复小数到分数计算器的 java.lang.StringIndexOutOfBoundsException

标签 java string substring

我是java新手,我正在尝试编写一个方法,该方法采用重复小数并将其转换为分数。它需要输入类似(不重复的 double 小数,要重复的尾随数字的 int 数),即 (0.3,1) 将是 0.3333.... 和 (1.583,2) 将是 1.5838383.... 我收到此错误,但我似乎无法找出问题所在。当前输入为(10.3,1)

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.substring(String.java:1967)
at Fraction.<init>(Fraction.java:44)
at Main.main(Main.java:12)
exited with non-zero status

这是我的代码:

public Fraction(double t, int repeating)
{
    double decRight = t, decLeft = t;
    String rStr = "0000000000000000000" + String.valueOf(decRight);
    String lStr = "0000000000000000000" + String.valueOf(decLeft);
    int count1 = 0, count2 = 0, rDecIndex = rStr.indexOf("."), lDecIndex = lStr.indexOf(".");
    while(!lStr.substring(lDecIndex - repeating,lDecIndex).replace("\\.","").equals(lStr)) //this is line 44, the problem area
    {
        decLeft *= 10;
        count1++;
        lStr = "0000000000000000000" + String.valueOf(decLeft);
        lDecIndex = lStr.indexOf(".");
    }
    while(!rStr.substring(rDecIndex,repeating).replace("\\.","").equals(rStr))
    {
        decRight*= 10;
        count2++;
        rStr = "0000000000000000000" + String.valueOf(decRight);
        rDecIndex = rStr.indexOf(".");
    }
    top = (int)(decLeft - decRight);
    bot = (int)(Math.pow(10,count1) - Math.pow(10,count2));
    reduce();
}

错误出现在子字符串中。

最佳答案

根据注释,您需要一个包含重复小数的整数和一个包含重复部分之前的整数。

我认为你已经解决了这个问题,而不是仅仅使用子字符串来获取你想要的字符串部分。

/**
 * Code By Loren CC-BY
 */
public class test
{
    public static void fraction(String t, int repeating)
    {
        int bot = 0;
        for (int i = 0; i < repeating; i++) {
            bot *= 10;
            bot += 9;
        }
        String dString = t;
        // Get the repeating part of the string as an int
        int repeat = Integer.valueOf(dString.substring(dString.length()-repeating));
        // Get the string from in front of the repeating number
        String front = dString.substring(0, dString.length()-repeating);
        // Convert it to a double
        double before = Double.valueOf(front);
        // Debugging information
        System.out.println("Before: "+ front+" " +before+" "+dString.substring(dString.length()-repeating));

        // Turn the before string into an int and compute the denominator such that 1.5 could become 15/10
        int count = 0;
        while (Math.abs(Math.round(before) - before) > 0.000001) {
            before *= 10;
            count++;
        }
        // Print debugging information
        System.out.println(count);
        // Compute the top of the combined fraction
        int top = ((int)before)*bot+repeat;

        bot = (int)Math.pow(10,count) * bot;
        System.out.println(top+" "+bot);
        // TODO: Reduce fraction
    }

    public static void main(String[] args) {
        fraction("0.3",1);
        fraction("1.580",2);
        fraction("0.34",1);
        fraction("0.34",2);
    }
}

注意:您不能传入 double 1.580,因为作为字符串,它是 1.58 而不是 1.580,因为省略了 0,这会扰乱函数。

关于重复小数到分数计算器的 java.lang.StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47396008/

相关文章:

javascript - 如何从字符串中的方法名称调用 javascript 函数?

c - 断线对象 - Arduino

在 C 中创建一个字符串数组失败,为什么?

java - 如何显示不同日期的循环文本

java - Android 4.0 ICS 将 HttpURLConnection GET 请求转为 POST 请求

Java:依赖字段初始化顺序是否有任何风险?

MySql 如何从 mediumtext 中查询子字符串?

java 子字符串未按预期工作

java - 静态对象数组

java - 在 Play Framework 中删除父实体时,不为子实体调用 Model.delete()