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

Ama*_*ora 2 java string syntax-error indexoutofboundsexception

在输入整数后输入字符串"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)
Run Code Online (Sandbox Code Playgroud)

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

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));
    }

}
Run Code Online (Sandbox Code Playgroud)

Pio*_*ski 6

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