Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
新手程序员需要建议:“;字符串索引超出范围";-JAVA_Java_String_Indexing_Range - Fatal编程技术网

新手程序员需要建议:“;字符串索引超出范围";-JAVA

新手程序员需要建议:“;字符串索引超出范围";-JAVA,java,string,indexing,range,Java,String,Indexing,Range,我对编程非常陌生,我遇到了一个错误,我相信这对于更有经验的人来说是一个很容易解决的问题 以下是我所拥有的: import java.io.*; import java.util.Scanner; public class ReadNamesFile { public static void main(String[] args) throws IOException { // make the names.csv comma-se

我对编程非常陌生,我遇到了一个错误,我相信这对于更有经验的人来说是一个很容易解决的问题

以下是我所拥有的:

    import java.io.*;
    import java.util.Scanner;

    public class ReadNamesFile
    {
        public static void main(String[] args) throws IOException {
        // make the names.csv comma-separated-values file available for reading
        FileReader f = new FileReader("names.csv");
        BufferedReader r = new BufferedReader(f);
        //
        String lastName="unknown", firstName="unknown", office="unknown";

        // get first line
        String line = r.readLine();

        // process lines until end-of-file occurs
        while ( line != null )
        {
           // get the last name on the line
           //
           // position of first comma
           int positionOfComma = line.indexOf(","); 
           // extract the last name as a substring
           lastName = line.substring(0,positionOfComma); 
           // truncate the line removing the name and comma
           line = line.substring(positionOfComma+1); 

           // extract the first name as a substring
           firstName = line.substring(0,positionOfComma); 
           // truncate the line removing the name and comma
           line = line.substring(positionOfComma+1);

           // extract the office number as a substring
           office = line.substring(0,positionOfComma);
           // truncate the line removing the name and comma
           line = line.substring(positionOfComma+2);
           //
           //        
           //
           // display the information about each person
           System.out.print("\nlast name = "+lastName);
           System.out.print("\t first name = "+firstName);
           System.out.print("\t office = "+office);
           System.out.println();
           //
           // get the next line
           line = r.readLine();
        }
    }
}
基本上,它会在.csv文件中找到姓氏、名字和办公室号码并打印出来

编译时不会出现任何错误,但运行时会出现:

java.lang.StringIndexOutOfBoundsException: String index out of range: 7
at java.lang.String.substring(String.java:1955)
at ReadNamesFile.main(ReadNamesFile.java:34)
在尝试做办公室号码部分之前,前两个(姓和名)打印得很好,但办公室号码似乎不起作用

有什么想法吗

编辑:谢谢所有的帖子,伙计们,不过我还是不太明白。有人能发布一些真正让人哑口无言的东西吗?我已经试着解决这个问题一个小时了,但我无法得到它。

来自:

(StringIndexOutOfBoundsException)-由字符串方法抛出到 指示索引为负数或大于 绳子

在您的情况下,对
.substring
的一个调用被赋予一个值,该值为
=
字符串的长度。如果第34行是注释,则是第34行上方的行。

来自:

(StringIndexOutOfBoundsException)-由字符串方法抛出到 指示索引为负数或大于 绳子

在您的情况下,对
.substring
的一个调用被赋予一个值,该值为
=
字符串的长度。如果第34行是注释,则是第34行上方的那一行。

您需要:

a) 如果找不到逗号(即,如果找不到和提取lastName和/或firstName字符串),请确保处理该情况

b) 确保“positionOfComma+N”的值永远不会超过字符串的长度

几个“if”块和/或“continue”语句就可以很好地完成这个任务;-)

您需要:

a) 如果找不到逗号(即,如果找不到和提取lastName和/或firstName字符串),请确保处理该情况

b) 确保“positionOfComma+N”的值永远不会超过字符串的长度


几个“if”块和/或“continue”语句就可以很好地完成这个任务;-)

您正确地找到了命令的
位置
,但随后该逻辑应用于
的原始值。当您删除姓氏和逗号时,
positionOfComma
不再正确,因为它适用于行的旧值。

您正确地找到了
positionOfComma
,但该逻辑适用于
行的原始值。删除姓氏和逗号后,
positionOfComma
不再正确,因为它适用于行的旧值。

让我们举个例子,看看您的代码有什么问题

例如:行:溢出,堆栈
{长度:14}

逐行获取程序语句-

int positionOfComma = line.indexOf(",");  // returns 9

lastName = line.substring(0,positionOfComma); // should be actually postionOfComma-1
现在lastName已溢出
。命令的位置有
9

line = line.substring(positionOfComma+1);
现在
堆栈

firstName = line.substring(0,positionOfComma); 

请求子字符串从09。但是
stack
的长度只有5。这将导致字符串索引超出范围异常。希望您能理解哪里做错了。

让我们举个例子,看看您的代码有哪些问题

int positionOfComma = line.indexOf(",");
例如:行:溢出,堆栈
{长度:14}

逐行获取程序语句-

int positionOfComma = line.indexOf(",");  // returns 9

lastName = line.substring(0,positionOfComma); // should be actually postionOfComma-1
现在lastName已溢出
。命令的位置有
9

line = line.substring(positionOfComma+1);
现在
堆栈

firstName = line.substring(0,positionOfComma); 
请求子字符串从09。但是
stack
的长度只有5。这将导致字符串索引超出范围异常。希望你明白自己做错了什么

int positionOfComma = line.indexOf(",");
这行代码可能找不到逗号,那么commma的位置将是-1。接下来,使用(0,-1)-eek对某个对象进行子串,难怪它会给出StringIndexOutOfBoundsException。使用类似于:

int positionOfComma = 0;
if(line.indexOf(",")!=-1)
{
   positionOfComma = line.indexOf(",");
}
有时你确实需要做很多检查,尤其是当数据被破坏时:(

)

PS我相信聪明的人可以让我的代码看起来破旧不堪,但我希望你能明白我的意思:)

这行代码可能找不到逗号,那么commma的位置将是-1。接下来,使用(0,-1)-eek对某个对象进行子串,难怪它会给出StringIndexOutOfBoundsException。使用类似于:

int positionOfComma = 0;
if(line.indexOf(",")!=-1)
{
   positionOfComma = line.indexOf(",");
}
有时你确实需要做很多检查,尤其是当数据被破坏时:(

)


PS我相信聪明的人会让我的代码看起来很糟糕,但我希望你能明白我的意思:)

注意哪一行是第34行会有帮助。建议:1)阅读错误消息。我喜欢这个错误消息。它来自哪里?这是什么意思?2) 使用调试器并在异常时停止;使其爆炸的当前值是什么?为什么它们是如此,而不是预期的“有效”值?请注意哪一行是第34行。建议:1)阅读错误消息。我喜欢这个错误消息。它来自哪里?这是什么意思?2) 使用调试器并在异常时停止;使其爆炸的当前值是什么?为什么它们是如此,而不是预期的“有效”值?