Java StringIndexOutOfBoundsException 字符串索引超出范围错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18916959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:29:56  来源:igfitidea点击:

StringIndexOutOfBoundsException String index out of range error

javastringsyntax-errorindexoutofboundsexception

提问by AmanArora

I am getting this error when I enter the String "s" after entring an integer.

在输入整数后输入字符串“s”时出现此错误。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(Unknown Source)
    at oneB.change(oneB.java:4)
    at oneB.main(oneB.java:26)

Following is the code: (Please note that the code is still complete and I have entered some print statements for checking)

以下是代码:(请注意代码仍然完整,我已经输入了一些打印语句进行检查)

import java.util.Scanner;
public class oneB {
    public static String change(int n, String s, String t) {

        if (s.charAt(0) == 'R') {
            return onetwo(s);
        }
        return s;
    }
    private static String onetwo(String one) {
        int c = one.indexOf('C');
        System.out.print(c);
        char[] columnarray = new char[one.length() - c - 1];
        for (int i = c + 1; i < one.length(); i++) {
            columnarray[i] = one.charAt(i);
        }
        int columnno = Integer.parseInt(new String(columnarray));
        System.out.print(columnno);
        return one;

    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System. in );
        int n = in .nextInt();
        String s = in .nextLine();
        String t = in .nextLine();
        System.out.print(change(n, s, t));
    }

}

采纳答案by Piotr Ko?aczkowski

The call in.nextInt()leaves the endline character in the stream, so the following call to in.nextLine()results in an empty string. Then you pass an empty string to a function that references its first character and thus you get the exception.

该调用in.nextInt()将结束行字符留在流中,因此以下调用会in.nextLine()产生一个空字符串。然后你将一个空字符串传递给一个引用它的第一个字符的函数,因此你得到了异常。

回答by Kayz

It looks like sis an empty String "".

看起来像是s一个空的 String ""

回答by Stephen C

Here's how I debugged it:

这是我调试它的方式:

  • You are getting a StringIndexOutOfBoundsExceptionwith index zero at line 4.

  • That means that the String you are operating on when you call s.charAt(0)is the empty String.

  • That means that s = in.nextLine()is setting sto an empty String.

  • StringIndexOutOfBoundsException在第 4 行得到一个索引为零的值。

  • 这意味着您在调用时正在操作的字符串s.charAt(0)是空字符串。

  • 这意味着s = in.nextLine()设置s为空字符串。

How can that be? Well, what is happening is that the previous nextInt()call read an integer, but it left the characters after the integer unconsumed. So your nextLine()is reading the remainder of the line (up to the end-of-line), removing the newline, and giving you the rest ... which is an empty String.

怎么可能?好吧,发生的事情是前一个nextInt()调用读取了一个整数,但它留下了未使用整数后面的字符。所以你nextLine()正在阅读该行的其余部分(直到行尾),删除换行符,并给你剩下的......这是一个空字符串。

Add an extra in.readLine()call before you attempt to read the line into s.

添加一个额外的in.readLine()尝试读取行成之前调用s

回答by Prabhakaran Ramaswamy

   for (int i = c + 1; i < one.length(); i++) {
        columnarray[i] = one.charAt(i);   // problem is here.
    }

You need to start array index from 0. But you are starting from c + 1

您需要从 0 开始数组索引。但是您是从 c + 1 开始的

    for (int i = c + 1,j=0; i < one.length(); i++,j++) {
        columnarray[j] = one.charAt(i);
     }

回答by SegFault

The problem is that when you hit enter, your int is followed by a '\n' character. Just modify the code like this :

问题是当你按下回车键时,你的 int 后面跟着一个 '\n' 字符。只需像这样修改代码:

public static void main(String[] args) {
    Scanner in = new Scanner(System. in );
    int n = in .nextInt();
    in.nextLine(); //This line consume the /n afer nextInt
    String s = in .nextLine();
    String t = in .nextLine();
    System.out.print(change(n, s, t));
}

回答by Sajan Chandran

One another solution to the problem would be instead of nextLine(), use just next().

该问题的另一种解决方案是nextLine()使用 just代替next()

        int n = in .nextInt();
        String s = in .next();