Java 正在接收StringIndexOutOfBoundsException,但无法找到源

Java 正在接收StringIndexOutOfBoundsException,但无法找到源,java,substring,Java,Substring,我的任务: 使用scanner方法从数据行中提取字符串、浮点和int 数据格式为: Random String, 240.5 51603 Another String, 41.6 59087 等等 我的源代码片段: import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class readTest { public

我的任务: 使用scanner方法从数据行中提取字符串、浮点和int

数据格式为:

Random String, 240.5 51603
Another String, 41.6 59087
等等

我的源代码片段:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    public class readTest { 

        public static void main(String args[]) throws FileNotFoundException
          {

            System.out.println ("Enter file name");

            Scanner scanInput = new Scanner(System.in); //Scanner for reading keyboard input for file name
            String fileName = scanInput.nextLine(); //Defines file name as a string from keyboard
            File inputTxt = new File(fileName); //Declares the file based on the entered string
            Scanner in = new Scanner(inputTxt);

            do {

            int a; //variable to count how many characters in name
            String baseStringA = in.nextLine(); //read whole line as string
            a = baseStringA.indexOf(","); //defines a as the posistion of the comma
            String reduceStringA = baseStringA.substring(0, a); //reduces string to everything before comma

            Scanner scanA = new Scanner(baseStringA).useDelimiter("[^.0-9]+"); //removes letters and comma from string
            Float numberA = scanA.nextFloat();
            int integerA = scanA.nextInt();

            System.out.print (reduceStringA + numberA + integerA);

            } while (in.hasNextLine());
}
}
因此,在研究了几个不同的主题后(我对任何类型的编码都很陌生),我终于成功地写出了这段代码。我非常激动,终于获得了我想要的输出。但是,在尝试实现一个循环以使该过程对所有可用行重复之后,我经常在程序打印第一行的输出后遇到错误java.lang.StringIndexOutOfBoundsException

完整错误:

String index out of range: -1
    at java.lang.String.substring(Unknown Source)
    at readTest.main(readTest.java:43)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
我试着做了一点调查,我相信这是我的观点

String reduceStringA = baseStringA.substring(0, a);
当我试图读取错误给出的实际信息时,这似乎更为明显,但当我试图跟踪程序出现问题的地方时,结果却是空的

有人能发现我的新手错误吗?还是我只是在这个过程中完全错误

输入txt:

Stringy String, 77.2  36229 
More Much String, 89.4 24812
Jolly Good String, 182.3 104570
这是我犯错误的一个例子

而输入

Random String, 240.5 51603
Another String, 41.6 59087
String String, 182.6 104570
按计划工作

这对我来说真的很奇怪

        int a; //variable to count how many characters in farm name
        String baseStringA = in.nextLine(); //read whole line as string
        a = baseStringA.indexOf(","); //defines a as the posistion of the comma
        String reduceStringA = baseStringA.substring(0, a);
如果
baseStringA
baseStringA.indexOf()
中没有逗号。因此,您将尝试获取子字符串(0,-1),从而得到错误


最后,错误来自这里
baseStringA.substring(0,a)
由于开始索引0中的一个大于结束索引a(即-1),因此当指定的字符不在
字符串的readTest.main(readTest.java:43)”中时,会返回
-1
,这一行是什么?异常会告诉您行号--readTest.java:43。不需要“研究”来识别故障线路。请阅读
indexOf
上的文档。如果找不到搜索参数,它会返回什么?因此它读取数据的方式肯定有问题,因为每行输入文本的字符串后面都有一个逗号。你的解释很有帮助,因为我知道我需要寻找什么。我只能猜测而没有看到文件。检查文件底部是否没有空行。显示第一行中的值后,以下输入文件将生成错误
Stringy String、77.2 36229 More More More String、89.4 24812 Jolly good String、182.3 104570
。我将把这段代码编辑到主帖子中,因为注释的格式对我来说已经不适用了。