为什么我会收到子字符串的运行时错误 - 线程异常 "main" java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:10

Why am I getting Runtime errors for substring - Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10

我希望能够使用子字符串方法,或者如果你们有更好的建议,关于如何从包含一些信息的文本文件中获取数字并能够除以 num2 乘以 100,然后将 num1 乘以 num2。我想了解如何正确使用子字符串方法。

这是我目前所拥有的,虽然它不是完整的代码(我让其他部分可以工作),但如果有人能提供帮助,我将不胜感激。 我也在使用一个数组列表。

文本示例:

1 : 4.74 (93 % pa)

2 : 1.03 (92 % pa)

3 : 2.95 (99 % pa)

4 : 5.18 (61 % pa)

5 : 5.50 (81 % pa)

6 : 3.24 (55 % pa)

7 : 3.66 (64 % pa)

8 : 3.44 (98 % pa)

9 : 2.36 (76 % pa)

10 : 1.78 (94 % pa)

编辑: 数字是小数和百分比

edit2:

edit3: 试图解决这个问题(如果你想让它更容易理解我的问题,请阅读这个)http://csta.villanova.edu:8080/bitstream/2378/323/1/Forestry.html

case "Y": {

        try {
            FileReader text = new FileReader(mu + ".txt");
            Scanner file = new Scanner(text);

            //now read the file line by line...
            int lineNum = 0;
            while (file.hasNextLine()) {
                String num1 = file.nextLine();
                String num2 = file.nextLine();
                String year1 = "";
                num1 = year1.substring(4,9);
                String year2 = "";
                num2 = year2.substring(10,13);

                try {
                    double nnum1 = Double.valueOf(num1.trim()).doubleValue();
                    double nnum2 = Double.valueOf(num2.trim()).doubleValue();
                        nnum2 /= 100;
                    nnum1 *= nnum2;
                } catch (NumberFormatException nfe) {
                     System.out.println("NumberFormatException: " + nfe.getMessage());
                }

                if(lineNum == projekt.size()) { 
                    num1 = year1.substring(5,10);
                    num2 = year2.substring(11,14);
                    try {
                        double nnum3 = Double.valueOf(num1.trim()).doubleValue();
                        double nnum4 = Double.valueOf(num2.trim()).doubleValue();
                            nnum4 /= 100;
                            nnum3 *= nnum4;
                    } catch (NumberFormatException nfe) {
                         System.out.println("NumberFormatException: " + nfe.getMessage());
                    }

                }
                lineNum++;
            }
            file.close();
        } catch(FileNotFoundException e) { 
            System.out.println("Nope");
        }
        //ecree = new PrintWriter(mu + ".txt");
        //height + height * percentage 
        // use substring to grab percentage from file
        // remember to divide the percentage by 100 to show
    }
    break;

我感觉你在调用 substring 错误的变量。

                String year1 = "";
                num1 = year1.substring(4,9);
                String year2 = "";
                num2 = year2.substring(10,13);

您将 year1 设置为“”,然后尝试将 num1 设置为“”的子字符串。

看起来你想要反过来。

a) 不确定扫描仪 class 的作用,但您似乎一次读取了 2 行。 而 (file.hasNextLine()) { 字符串 num1 = file.nextLine(); 字符串 num2 = file.nextLine();

b) 你不应该覆盖你的数据:你将文本读入 num1 但你将空字符串 (year1) 的一部分分配给它,这肯定会产生索引越界异常: 字符串 num1 = file.nextLine(); 字符串 year1 = ""; num1 = year1.substring(4,9);

你是说也许

                String num1 = file.nextLine();
                String year1 = num1.substring(4,9);

c) 您应该使用 java 模式匹配以获得更通用的方法(参见 https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

                Pattern p = Pattern.compile("(\d+)\s*\:\s*(\d+\.\d*)\s*\((d+).+");
                String num1 = file.nextLine();
                String year1 = o.match(num1.trim());