我的任务:
使用扫描仪方法从数据行中提取字符串,浮点数和整数。

数据格式为:

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() will return -1中是否没有逗号。因此,您将尝试获取子字符串(0,-1),从而得到错误。

最终错误来自这里baseStringA.substring(0, a);,因为开始索引0之一大于结束索引a(它为-1)-更多here

关于java - 接收StringIndexOutOfBoundsException但无法找到源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28906295/

10-12 05:20