有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

新手程序员需要建议:“字符串索引超出范围”Java

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

以下是我所拥有的:

    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)

在尝试做办公室号码部分之前,前两个(姓和名)打印得很好,但办公室号码似乎不起作用

有什么想法吗

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


共 (5) 个答案

  1. # 1 楼答案

    JavaDoc

    (StringIndexOutOfBoundsException) - Thrown by String methods to indicate that an index is either negative or greater than the size of the string.

    在您的例子中,对.substring的一个调用被赋予了一个值,该值是>=字符串的长度。如果第34行是注释,则是第34行上方的行

  2. # 2 楼答案

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

    Eg: line: Overflow,stack
    { length: 14 }

    逐行记录程序语句-

    int positionOfComma = line.indexOf(",");  // returns 9
    
    lastName = line.substring(0,positionOfComma); // should be actually postionOfComma-1
    

    现在lastName有Overflow。命令的位置有9

    line = line.substring(positionOfComma+1);
    

    现在linestack

    firstName = line.substring(0,positionOfComma); 
    

    请求子字符串从09。但是stack的长度只有5。这将导致字符串索引超出范围异常。希望你明白自己做错了什么

  3. # 3 楼答案

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

  4. # 4 楼答案

    您需要:

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

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

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

  5. # 5 楼答案

    int positionOfComma = line.indexOf(",");
    

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

    int positionOfComma = 0;
    if(line.indexOf(",")!=-1)
    {
       positionOfComma = line.indexOf(",");
    }
    

    有时你确实需要做很多检查,尤其是当数据被破坏时:(

    http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#indexOf(java.lang.String

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