Java String方法将字符串转换为字符,然后显示具有指定索引的字符
我试图编写一个关于字符串方法的代码,该方法接受字符串并返回getChars的值。索引应该是用户的输入。字符串应转换为将用作目标的字符数组。然而,我有一些令人困惑的错误 有什么帮助吗? 这是代码/Java String方法将字符串转换为字符,然后显示具有指定索引的字符,java,arrays,string,Java,Arrays,String,我试图编写一个关于字符串方法的代码,该方法接受字符串并返回getChars的值。索引应该是用户的输入。字符串应转换为将用作目标的字符数组。然而,我有一些令人困惑的错误 有什么帮助吗? 这是代码/ import java.util.*; public class Exercise5 { public static void main(String[] args) { //Stay Calm and Be Cool! Scanner a = n
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));
}
}
错误:不允许使用void--如何消除该问题?StringgetChars不返回任何值,它将结果写入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);
What char[]sample=string.toCharArraysample;应该怎么办?您正在尝试调用哪个方法?你能链接它的JavaDoc吗,因为我没有看到任何接受char[]的toCharArray。对不起,这个示例不能在那个paradensis中。。。