String方法 - 将字符串转换为char,然后显示具有指定索引的字符

时间:2018-03-11 10:38:14

标签: java arrays string

我试图创建一个关于接受字符串的字符串方法的代码并返回getChars的值。索引或索引应该是用户的输入。字符串应转换为将用作目标的字符数组。但是,我有一些令人困惑的错误。

有任何帮助吗? 这是代码/.

    import java.util.*;
    public class Exercise5 {
    public static void main(String[] args) {
        //Stay Calm and Be Cool!
        Scanner a = new Scanner(System.in);
        String string = new String();

        System.out.print("Enter any String: ");
        string = a.nextLine();

        System.out.print("Enter source begin index: ");
        int begin = a.nextInt();

        System.out.print("Enter source end index: ");
        int end = a.nextInt();

        System.out.print("Enter destination index: ");
        int dest = a.nextInt();

        char[] sample = string.toCharArray();
        System.out.println("The value extracted from " + string + " with indexes "+ begin + " to "+ end + " is: " + 
                string.getChars(begin, end, sample, dest));
    }

}

错误:不允许无效。 ---怎么摆脱那个?

1 个答案:

答案 0 :(得分:1)

dest不返回任何值,它将结果写入final int length = end - begin; char[] sample = new char[length]; string.getChars(begin, end, sample, dest); System.out.println("The value extracted from " + string + " with indexes "+ begin + " to "+ end + " is: " + sample); 参数:

$ cd ~/.nvm/versions/node/v6.13.1/lib/node_modules/kapsel/cli
$ nvm use 4.8.7
$ npm install -g
$ ls ~/.nvm/versions/node/v4.8.7/lib/node_modules/kapsel
~/.nvm/versions/node/v4.8.7/lib/node_modules/kapsel